2016-10-27 21 views
1

오류 : || === Davis_project1의 디버그 (컴파일러 : GNU GCC 컴파일러) === |struct tm errror의 재정의. 헤더가있는 문제

C : \와 Mingw \ \ time.h 포함 | 172 | 오류 : '구조체 TM'의 재정을 | '구조체 TM'의 이전 정의를 | :

C는 : | | 87 오류를 \와 Mingw \는 wchar.h \ 포함

|| === 실패 구축 : 2 오류 (들), 0 경고 (들) (0 minute (s)을 0 초 (들)) === | 나는부터 srand 함수의 ctime이 헤더를 추가하려고 할 때까지

내 프로그램은 완전히 잘 실행되었다. 그 이후로 나는 'struct tm'과 'struct tm'오류의 이전 정의에 대한 재 정의를 얻습니다. 인터넷은 나에게 헤더 문제를 알려줍니다. 여기 아래의 모든 헤더와 cpp가 있습니다. 어떤 충고가 apppreciated. 다음은 내 메인 cpp입니다. 나는 문제가 여기

#include <iostream> 
#include <string> 
#include "Club.h" 
#include <ctime> 
using namespace std; 

int main(){ 

Club Club1; 
Club1.displayMenu(); 
Club1.processInput(); 
} 

Cbros 헤더

#ifndef CBROS_H 
#define CBROS_H 

#include <iostream> 

class Cbros 
{ 
public: 

    Cbros(); 
    //default constructor 
    Cbros(std::string , int , double); 

    std::string getName()const; 
    int getAge()const; 
    int getStr()const; 
    int getCharm()const; 
    int getIntel()const; 
    int getHeight()const; 
    void setName(std::string); 
    void setAge(int); 
    void setHeight(int); 
    void setIntel(int); 
    void setStr(int); 
    void setCharm(int); 
    void MeetGirl(int, int, int); 
    void GirlChance(int, int, int); 








    class Wallet{ 
public: 
    double addMoney(double amount);//Adds Money to wallet 
    double removeMoney(double amount);//removes money from wallet 
    int countMoney();//displays money in wallet 
protected: 
    int money; 

}; 
Wallet Mywallet; 



protected: 

    std::string newName; 
    std::string newRace; 
    int newAge; 
    double newHeight; 
    int newStr; 
    int newInt; 
    int newCharm; 





private: 

}; 



#endif // CBROS 

Cbros의 CPP

#include "Cbros.h" 


Cbros::Cbros() 
{ 
} 
Cbros::Cbros(std::string name ,int age , double height) 
:newName(name),newAge(age),newHeight(height) 
{ 
} 

std::string Cbros::getName() const{ 
return newName; 
} 

int Cbros::getAge() const{ 
return newAge; 
} 
int Cbros::getHeight() const{ 
return newHeight; 
} 

int Cbros::getIntel() const{ 
return newInt; 
} 

int Cbros::getCharm() const{ 
return newCharm; 
} 
int Cbros::getStr() const{ 
return newStr; 
} 

void Cbros::setName(std::string name){ 
newName=name; 
} 


void Cbros::setAge(int age){ 
newAge=age; 
} 

void Cbros::setHeight(int height){ 
newHeight=height; 
} 
void Cbros::setCharm(int charm){ 
newCharm=charm; 
} 
void Cbros::setIntel(int intel) { 
newInt=intel; 
} 
void Cbros::setStr(int str){ 
newStr=str; 
} 

double Cbros::Wallet::addMoney(double amount){if (amount < 0) 
    return 0; 
money += amount; 
return amount; 
} 
double Cbros::Wallet::removeMoney(double amount) { 
if ((amount < 0) || (money < amount)) 
    return 0; 
money -= amount; 
return amount; 
} 
int Cbros::Wallet::countMoney() { 
return money; 
} 
void Cbros::MeetGirl(int newStr, int newCharm, int newInt){ 

if (newHeight>=60){ 

    std::cout<<"Mmmm I like my men tall, Hi, I'm Brittany"; 

} 

} 
void Cbros::GirlChance(int newStr, int newCharm, int newInt){ 

} 

클럽 헤더

#ifndef CLUB_H 
#define CLUB_H 
#include "Cbros.h" 


class Club: public Cbros 
{ 
public: 
    Club(); 
    int displayMenu(); 
    void processInput(); 
    int menuSelection; 


protected: 


private: 


}; 

#endif // CLUB_H 

클럽 CPP

생각 Cbros에서 16,
class Cbros; 
class Jock; 
class Artsy; 
class Engineering; 

#include "Club.h" 




Club::Club() 
{} 


int Club::displayMenu(){ 
std::cout<<"Welcome Meet a Girl Game!"<<std::endl; 
std::cout<<"The goal is to use your inherent skills to earn a girl's phone number\n\n"; 
std::cout<<"Please enter the number of your choice:\n\n"; 
std::cout<<"1. Go the Club\n"; 
std::cout<<"2. Stay at Home\n"; 

std::cin>>menuSelection; 
return menuSelection; 





} 
void Club::processInput(){ 
while (menuSelection!=1&&menuSelection!=2){ 
    std::cout<<"invalid choice\n"<<displayMenu(); 

} 
if (menuSelection==1){ 
    std::cout<<"Are you a\n"; 
    std::cout<<"1. Engineer Bro?\n"; 
    std::cout<<"2. Artsy Bro?\n"; 
    std::cout<<"3. Jock Bro?\n"; 
    std::cin>>menuSelection; 

    switch(menuSelection){ 

case 1:{ 
    std::cout<<"Welcome Engineer Bro\n"; 
    std::cout<<"Please enter your name:"; 
    std::cin.ignore(); 
    std::getline(std::cin, newName); 
    std::cout<<"Please enter your Height(in inches):"; 
    std::cin>>newHeight; 
    std::cout<<"Please enter your Age:"; 
    std::cin>>newAge; 
    Cbros Engine(newName, newAge, newHeight); 
    Engine.setCharm(3); 
    Engine.setStr(3); 
    Engine.setIntel(6); 
    std::cout<<std::string(20,'\n'); 


    std::cout<<"Class: Engineer\n"; 
    std::cout<<"Name: "<<Engine.getName()<<std::endl; 
    std::cout<<"Height: "<<Engine.getHeight()<<std::endl; 
    std::cout<<"Age "<<Engine.getAge()<<std::endl; 
    std::cout<<"Initial Stats"<<std::endl; 
    std::cout<<"Charm: "<<Engine.getCharm()<<std::endl; 
    std::cout<<"Strength: "<<Engine.getStr()<<std::endl; 
    std::cout<<"Intelligence: "<<Engine.getIntel() <<std::endl<<std::endl<<std::endl<<std::endl; 
    std::cout<<"Welcome to Club CSC 1310\n"; 
    std::cout<<"No time to waste lets meet some girls!\n"; 
    std::cout<<"Hmmm I think I see someone they may be your type over there"; 
    Engine.MeetGirl(3,3,6); 




    break; 
    } 

case 2:{ 
    std::cout<<"Welcome Artsy Bro!\n"; 
    std::cout<<"Please enter your name:"; 
    std::cin.ignore(); 
    std::getline(std::cin, newName); 
    std::cout<<"Please enter your Height(in inches):"; 
    std::cin>>newHeight; 
    std::cout<<"Please enter your Age:"; 
    std::cin>>newAge; 
    Cbros Artsy(newName, newAge, newHeight); 
    Artsy.setCharm(6); 
    Artsy.setStr(3); 
    Artsy.setIntel(3); 
    std::cout<<"Artsy Bro's Name: "<<Artsy.getName()<<std::endl; 
    std::cout<<"Height: "<<Artsy.getHeight()<<std::endl; 
    std::cout<<"Age "<<Artsy.getAge()<<std::endl; 
    std::cout<<"Initial Stats"<<std::endl; 
    std::cout<<"Charm: "<<Artsy.getCharm()<<std::endl; 
    std::cout<<"Strength: "<<Artsy.getStr()<<std::endl; 
    std::cout<<"Intelligence: "<<Artsy.getIntel()<<std::endl; 

    break; 
    } 
case 3: { 

    std::cout<<"Welcome Jock Bro!\n"; 
    std::cout<<"Please enter your name:"; 
    std::cin.ignore(); 
    std::getline(std::cin, newName); 
    std::cout<<"Please enter your Height(in inches):"; 
    std::cin>>newHeight; 
    std::cout<<"Please enter your Age:"; 
    std::cin>>newAge; 
    Cbros Jock(newName, newAge, newHeight); 
    Jock.setCharm(3); 
    Jock.setStr(6); 
    Jock.setIntel(3); 
    std::cout<<"Jock Bro's Name: "<<Jock.getName()<<std::endl; 
    std::cout<<"Height: "<<Jock.getHeight()<<std::endl; 
    std::cout<<"Age "<<Jock.getAge()<<std::endl; 
    std::cout<<"Initial Stats"<<std::endl; 
    std::cout<<"Charm: "<<Jock.getCharm()<<std::endl; 
    std::cout<<"Strength: "<<Jock.getStr()<<std::endl; 
    std::cout<<"Intelligence: "<<Jock.getIntel()<<std::endl; 



    break; 
    } 
default: 


    break; 
    } 
} 
else if(menuSelection==2){ 
    std::cout<<"Enjoy Netflix Bro";} 
} 

세 가지 다른 파생 클래스는, 그들 모두는 다음과 같은 .H 및 .CPP 설정을 가지고있다.

Jock Header 

#ifndef JOCK_H 
#define JOCK_H 
#include "Cbros.h" 

조크 CPP

#include "Jock.h" 

jock::Jock() 
{ 

} 

엔지니어 헤더

#ifndef ENGINEERING_H 
#define ENGINEERING_H 
#include "Cbros.h" 

엔지니어 CPP

class Engineering: public Cbros 
{ 
public: 
    Engineering(); 

protected: 
private: 

}; 

예술 헤더

#ifndef ARTSY_H 
#define ARTSY_H 

#include "Cbros.h" 

class Artsy : public Cbros 
{ 
public: 
    Artsy(); 



protected: 


private: 
}; 

#endif // ARTSY_H 

예술의 CPP

#include "Artsy.h" 

Artsy::Artsy() 
{ 

} 
+1

끄기 주제 : 일반적인 nag. 2016 년입니다.'srand'를 잠자리에 들일 때입니다. [ 대신에.] (http://en.cppreference.com/w/cpp/numeric/random) – user4581301

+1

너무 많은'struct tm'을 통해 컴파일을 거부하는 cpp 파일을 좁히는 데 도움이되는 또 다른 편집이 필요합니다. 에스. – user4581301

+0

언제든지 Google 난수 생성기, 그 예제를 얻을.나는 을 살펴볼 것이다. 감사! –

답변

1

이것은 컴파일러의 문제가, VC++이 문제가되지 않습니다.

구조체 tm은 이미 wchar.h 파일 (cstring과 함께 제공됨)에 정의되어 있습니다. 이 오류를 해결하려면 getpid()를 시드로 사용할 수 있습니다.

// Both header-files are necessary for 'getpid()' 
#include <sys/types.h> 
#include <unistd.h> 

srand(getpid()); 

pid 또는 progess-id는 프로그램을 실행하는 동안 변경되지 않으므로 시드를 한 번만 설정하십시오.