1

Error: ||=== Build: Debug in Davis_project1 (compiler: GNU GCC Compiler) ===|

c:\mingw\include\time.h|172|error: redefinition of 'struct tm'|

c:\mingw\include\wchar.h|87|error: previous definition of 'struct tm'|

||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

My program was running completely fine until I tried to add the ctime header for a srand function. Since then I get redefintion of 'struct tm' and previous definition of 'struct tm' errors. Internet tells me its a header problem. So here are all my headers and cpp's below. Any advice apppreciated. Below is my main cpp. I think the issue is here

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

int main(){

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

Cbros header

#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){

}

Club Header

#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

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

Three other Derived classes from Cbros, they all have the same .h and .cpp setup as below.

Jock Header

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

Jock cpp

#include "Jock.h"

jock::Jock()
{

}

Engineer Header

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

Engineer cpp

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

protected:
private:

};

Artsy header

#ifndef ARTSY_H
#define ARTSY_H

#include "Cbros.h"

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



protected:


private:
};

#endif // ARTSY_H

Artsy cpp

#include "Artsy.h"

Artsy::Artsy()
{

}
Kelvin Davis
  • 355
  • 1
  • 3
  • 10
  • 1
    Off topic: Usual nag. It's 2016. Time to put `srand` to bed. [Use `` instead.](http://en.cppreference.com/w/cpp/numeric/random) – user4581301 Oct 27 '16 at 06:09
  • 1
    Worth another edit to narrow things down to which cpp file refuses to compile over the too many `struct tm`s. – user4581301 Oct 27 '16 at 06:11
  • Whenever i google random number generator, its the example i get. I'll look into . Thanks! – Kelvin Davis Oct 27 '16 at 06:12
  • @user4581301 The error doesnt say where, it simpy brings up a seperate header file for time.h and wchar.h which isnt in my project and takes me to line 87 struct tm { and line 172 struct tm. – Kelvin Davis Oct 27 '16 at 06:16
  • Every cpp file is compiled separately, so if you read back a bit in the build output it should tell you which cpp file is being compiled when the error is raised. – user4581301 Oct 27 '16 at 06:18
  • 1
    If you use GCC (`g++`), then add the `-H` option to list the included headers (which identifies which header includes which other headers). – Jonathan Leffler Oct 27 '16 at 06:18
  • @JonathanLeffler Looks like it happens when i compile the main.cpp it says multiple uses of struct tm from, time.h and wchar.h – Kelvin Davis Oct 27 '16 at 06:23
  • You may need to track back to the top-level header that you include, but `` is a likely candidate, and work out what goes wrong. You may need to look at the output of the preprocessor. – Jonathan Leffler Oct 27 '16 at 06:26
  • It should not normally happen, no matter what system headers and in what order you include. What is your platform and compiler version? – n. m. could be an AI Oct 27 '16 at 06:35
  • 1
    This should not be closed as a duplicate of that question, or any other similar question. No amount of circular dependencies between *user's* headers should lead to a redefinition of `struct tm` from a *system* header. – n. m. could be an AI Oct 27 '16 at 06:56
  • @n.m. platform is Code Blocks and compiler version is GNU GCC Compiler, using the c++11 version im pretty sure. – Kelvin Davis Oct 27 '16 at 08:10
  • @JonathanLeffler When I run the -H in my compiler options, its says, suggestions: use header guards for ctime, so im guessing thats the problem. Just need to figure where to place guards. – Kelvin Davis Oct 27 '16 at 08:15
  • Can you post your entire `ctime` file to something like pastebin.com? – n. m. could be an AI Oct 27 '16 at 08:19
  • @n.m I've editted my main cpp file above to show whats in there. The issue is that im including "Club.h" in the main. But i need that there to be able to create an Object of Club to start to game. suggestions? – Kelvin Davis Oct 27 '16 at 08:32
  • Please post a **minimal reproducible** example. You have `#ifndef` without `#endif` in posted headers, which cannot possibly work, You also have posted irrelevant files that you are not even compiling. Please make sure **you** can copy your post, paste it to a file or several files without making any changes, invoke the compiler, and get the same error you are getting otherwise. If you can't do that, your posted code has no useful content. – n. m. could be an AI Oct 27 '16 at 08:47
  • @n.m. Took a bit, but the whole code is there that produces the error. – Kelvin Davis Oct 27 '16 at 09:21
  • Why are you posting all the derived classes? You get a compilation error while compiling **one** cpp file. Post it and all the headers it **needs**, nothing more. – n. m. could be an AI Oct 27 '16 at 09:25
  • Also **PLEASE DO MAKE SURE** you post exactly what you compile and not some manually edited version thereof. Where is the clising brace in main.cpp? What does `Class.cpp` do in the Club header? – n. m. could be an AI Oct 27 '16 at 09:29
  • @n.m Sorry wasnt sure if you wanted to run a test yourself or not. Giving tmi, apologies. Ive posted the exact error codes. – Kelvin Davis Oct 27 '16 at 09:47
  • I cannot reproduce the problem.Copy your project to a fresh directory and compile with a command line "g++ -std=c++11 main.cpp". Do you still get the error? – n. m. could be an AI Oct 27 '16 at 11:37

1 Answers1

1

This is a problem of your compiler, VC++ doesn't have this problem.

The struct tm is already defined in the wchar.h file (coming with cstring). To solve this error you could use getpid() as seed.

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

srand(getpid());

The pid or progess-id doesn't change during the exection of your programm, so set the seed only once.

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225