Headers with Class Objects? error! with multiple files

This is a discussion on Headers with Class Objects? error! with multiple files within the C++ Programming forums, part of the General Programming Boards category; Heya, So I have a project with multiple files of course.. many headers which include other headers, which is causing ...

  1. #1
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Headers with Class Objects? error! with multiple files

    Heya,

    So I have a project with multiple files of course.. many headers which include other headers, which is causing "multiple definitions of" errors (I know thats the cause) .. I have 'ifndef' on all the headers and I'm not including any .cpp files, and I've done extensive testing.

    Basicly I have objects of a class that is required in most of my .cpp files, but I cant put those in a header to include to them or it will give me that error. I cant include a .cpp with the objects because that gives me an error too. I'm kinda stuck here.. I cant define the objects like you do with ints and such.

    typical situation:
    Code:
    ###### example.h
    
    #ifndef _EXAMPLE_H_
    #define _EXAMPLE_H_
    
    int problem;
    
    FruitClass RedFruit(); // error
    FruitClass GreenFruit(); // error
    
    #endif // _EXAMPLE_H_
    
    ###### example.cpp
    
    problem = 5;
    So the int is fine.. that works, but the class objects there will work but will return an error with multiple files (and the brackets dont matter so thats not it).

    Anyone mind lending me ideas?

    Thanks in advance,

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Dae
    typical situation:
    Code:
    ###### example.h
    
    #ifndef _EXAMPLE_H_
    #define _EXAMPLE_H_
    
    int problem;
    
    FruitClass RedFruit(); // error
    FruitClass GreenFruit(); // error
    
    #endif // _EXAMPLE_H_
    
    ###### example.cpp
    
    problem = 5;
    So the int is fine.. that works, but the class objects there will work but will return an error with multiple files (and the brackets dont matter so thats not it).

    Anyone mind lending me ideas?

    Thanks in advance,
    1) You don't call default constructors like that.

    2) It looks like you're trying to create some global variables for your program, but when the compiler sees the name FruitClass in these lines:

    FruitClass RedFruit();
    FruitClass GreenFruit();

    the compiler has no idea what FruitClass is unless you include FruitClass.h before those lines are included, so that could be another problem.

    3) If you put those lines in every file, it's equivalent to doing this:
    Code:
    int main()
    {
            int num;
            int num;
            int num;
    
    	return 0;
    }
    and the compiler won't let you do that and it will give you redfinition errors.

    4)Lastly, don't use global variables--problem solved. If a function needs a variable, then list it as a parameter, and send the variable to the function when you call the function. Using global variables means your functions will have external dependencies, which is generally bad program design. You want your functions to be stand alone black boxes, where you feed them some data, they do something, and then they return some data. In that way, your functions can be reused in any program.

    Here is a simple one file program:
    Code:
    #include <iostream>
    using namespace std;
    
    class Apple
    {
    private:
    	int size;
    
    public:
    	
    	Apple()
    	{
    		size = 1;
    	}
    
    	void show()
    	{
    		cout<<size<<endl;
    	}
    };
    
    Apple a;  //global variable
    
    int main()
    {
    	a.show(); //1
    	
    	return 0;
    }
    Dividing the program into multiple files:
    Code:
    //Apple.h
    
    class Apple
    {
    private:
    	int size;
    
    public:
    	
    	Apple();
    	void show();
    	
    };
    Code:
    //Apple.cpp
    
    #include <iostream>
    #include "Apple.h"
    using namespace std;
    
    
    Apple::Apple()
    {
    	size = 1;
    }
    
    void Apple::show()
    {
    	cout<<size<<endl;
    }
    Code:
    //main.cpp
    
    #include "Apple.h" //makes the name Apple known to the compiler
    
    Apple a;
    
    int main()
    {
    
    	a.show(); //1
    	
    	return 0;
    }
    Accessing the global variable Apple a from another file:
    Code:
    //Apple.h
    
    class Apple
    {
    private:
    	int size;
    
    public:
    	
    	Apple(int s);
    	Apple();
    	
    	int get_size();
    	void show();
    	
    };
    Code:
    //Apple.cpp
    
    #include <iostream>
    #include "Apple.h"
    using namespace std;
    
    extern Apple a;  //an instruction to look outside this 
    			//file for the definition of 'a'
    
    Apple::Apple(int s)
    {
    	size = s;
    }
    
    Apple::Apple()
    {
    	size = 1;
    }
    
    int Apple::get_size()
    {
    	return size;
    }
    
    void Apple::show()
    {
    	
    	cout<< a.get_size() + size<<endl; /****/
    }
    Code:
    //main.cpp
    #include <iostream>
    #include "Apple.h" //makes the name Apple known to the compiler
    
    using namespace std;
    
    Apple a;  //global variable
    
    int main()
    {
    
    	a.show(); //1
    
    	Apple b(10);
    	cout<<endl;
    	b.show(); //11
    
    	
    	return 0;
    }
    To put Apple a in its own header file, just replace the Apple a declaration with an #include statement.
    Code:
    //main.cpp
    #include <iostream>
    #include "Apple.h" //makes the name Apple known to the compiler
    #include "globals.h" 
    
    using namespace std;
    
    int main()
    {
    
    	a.show(); //1
    
    	Apple b(10);
    	cout<<endl;
    	b.show(); //11
    	
    	return 0;
    }
    Code:
    //globals.h
    
    Apple a;
    etc.
    Last edited by 7stud; 06-18-2005 at 11:38 AM.

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Thanks for the reply,

    Yeah it was just a quick example, not what my actual code looks like so 1) and 2) are off the table. Its 3) that I said (redefinition errors) that is giving me the problem, I know that, so your solution is 4).. to not use global variables. Aight..

    I'll redesign it, its just I have 5 .cpp's and some .h's I've been moving around the code to make it more sensible for editing. That wouldnt be a problem, however I'm doing a Win32 app where variables are being passed around with SendMessage and those all the time from other classes so I just wanted to define them once and no more. Guess that wont happen.. gonna have to define the class objects in every .h and add return functions for the variables in classes for.... all of them? alright..

    I'll go redesign, thanks for the info.. hoped there was a way around it :P..

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    hoped there was a way around it
    I posted a way around it: "extern".

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Ahh missed that, thanks that worked!

    So yeah bad code design so I wont use it often, its just sort of necessary when I'm trying to retrieve information of a certain instance of a class when using windows. I'm not using it in functions that do something either, its just for the inputting data part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 10:37 PM
  3. WM_COPYDATA and mutex selecting multiple files
    By gh0st in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2006, 02:22 PM
  4. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  5. base class pointer to derived class objects
    By curlious in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2003, 08:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21