【问题标题】:Redefinition of struct tm errror. Issues with headers重新定义 struct tm 错误。标题问题
【发布时间】:2016-10-27 06:04:48
【问题描述】:

错误:||=== 构建:在 Davis_project1 中调试(编译器:GNU GCC 编译器)===|

c:\mingw\include\time.h|172|错误:重新定义'struct tm'|

c:\mingw\include\wchar.h|87|错误:'struct tm'的先前定义|

||=== 构建失败:2 个错误,0 个警告(0 分钟,0 秒)===|

在我尝试为 srand 函数添加 ctime 标头之前,我的程序运行良好。从那以后,我重新定义了 'struct tm' 和以前定义的 'struct tm' 错误。互联网告诉我它的标题问题。所以这是我所有的标题和下面的cpp。任何建议表示赞赏。 下面是我的主要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

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";}
}

来自 Cbros 的其他三个派生类,它们都具有相同的 .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()
{

}

【问题讨论】:

  • 离题:通常的唠叨。现在是 2016 年。是时候让srand 睡觉了。 Use &lt;random&gt; instead.
  • 值得再次编辑以缩小 cpp 文件拒绝编译的范围,因为 struct tms 太多。
  • 每当我使用谷歌随机数生成器时,我都会得到示例。我会调查。谢谢!
  • 如果您使用 GCC (g++),则添加 -H 选项以列出包含的标头(标识哪个标头包含哪些其他标头)。
  • 这不应作为该问题或任何其他类似问题的副本而关闭。 user's 标头之间的任何循环依赖都不应该导致从 system 标头重新定义 struct tm

标签: c++ header-files redefinition


【解决方案1】:

这是你编译器的问题,VC++没有这个问题。

struct tm 已经在 wchar.h 文件中定义(随 cstring 一起提供)。要解决此错误,您可以使用 getpid() 作为种子。

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

srand(getpid());

pid 或 progess-id 在程序执行期间不会更改,因此只需设置一次种子。

【讨论】:

    猜你喜欢
    • 2017-12-08
    • 2017-09-01
    • 2021-07-07
    • 1970-01-01
    • 2013-10-27
    • 2011-07-20
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多