Why does a #include make my program not work.

Hello everyone. I'm getting pre processor directives mixed up. I'm not really sure what is wrong. I feel like I'm including everything correctly.

This is the file that won't compile. Note that it won't compile unless I comment out the line shown below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "queue.h"
#include "geneologylist.h" // Commenting out this should fix it

// NOTE THAT ALL OTHER CODE IS ALREADY COMMENTED OUT

//void Geneology::level()
//{
  //const int MAX = 100;
  //BTree *queue[MAX];
  //BTree *temp;
  //int front = 0;
  //int back = 0;

  //queue[back++] = this;

  //while (front != back)
  //{
  //  temp = queue[front];
  //  front = (front + 1) % MAX;
  //  if (temp != NULL)
  //  {
  //    // visit
  //    cout.width(4);
  //    cout << temp->data << " ";
  //    // end Visit        
  //    queue[back] = temp->left;
  //    back = (back + 1) % MAX;
  //    queue[back] = temp->right;
  //    back = (back + 1) % MAX;

  //  }
  //}
//}


#endif 



So here is the content of the GeneologyList.h

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

#include "list.h"
#include <string>
#include <cassert>
#include <fstream>
#include <iostream>
#include <exception>
using namespace std;

class Geneology : public List <Ancestor>
{
	// Some Content Here	
};


// Some Code definitions here

#endif 



I'm kinda used to Visual Studio doing all the work for me. I also do a lot of code in header files. I wanted to try making a separate file for class member function definition. Now it's not working? What am I doing wrong? Please ask if you need more information.
In GeneologyList.h, where did you define the class Ancestor?

Also note that the include-path is case-sensitive so geneologylist.h is something different than GeneologyList.h

This is the file that won't compile.
giving us the compiler errors would help a lot
Okay

This is the Ancestor file and class definition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#ifndef ANCESTOR_H
#define ANCESTOR_H


using namespace std;
#include <string>
#include <iostream>

struct Ancestor
{
	string lastName; // AKA surname
	string givenName; // AKA first name
	string birthYear;
	string birthMonth;
	string birthDay;

	int id;

	friend ostream & operator << (ostream & os, const Ancestor & rhs)
	{
		os << rhs.givenName;
		if (rhs.lastName != "" && rhs.givenName != "")
			os << " ";
		os << rhs.lastName;

		if (rhs.birthYear != "")
		{
			os << ", b. ";
			if (rhs.birthDay != "")
				os << rhs.birthDay << " ";
			if (rhs.birthMonth != "")
				os << rhs.birthMonth << " ";
			os << rhs.birthYear;
		}
		os << endl;
		return os;
	}
	bool operator <(const Ancestor & rhs);
};

#endif 




Here is the code output.



1>------ Build started: Project: Geneology, Configuration: Debug Win32 ------
1>  level.cpp
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(19): error C2065: 'Ancestor' : undeclared identifier
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(19): error C2923: 'List' : 'Ancestor' is not a valid template type argument for parameter 'T'
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(20): error C2955: 'List' : use of class template requires template argument list
1>          c:\users\cameron\desktop\235\geneology - final project\list.h(86) : see declaration of 'List'
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(28): error C2065: 'Ancestor' : undeclared identifier
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(28): error C2923: 'Node' : 'Ancestor' is not a valid template type argument for parameter 'T'
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(33): error C2065: 'Ancestor' : undeclared identifier
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(33): error C2923: 'Node' : 'Ancestor' is not a valid template type argument for parameter 'T'
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(22): error C2065: 'Ancestor' : undeclared identifier
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(22): error C2923: 'List' : 'Ancestor' is not a valid template type argument for parameter 'T'
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(22): error C2614: 'Geneology' : illegal member initialization: 'List' is not a base or member
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(52): error C2065: 'Ancestor' : undeclared identifier
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(52): error C2923: 'Node' : 'Ancestor' is not a valid template type argument for parameter 'T'
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(53): error C2065: 'Ancestor' : undeclared identifier
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(53): error C2923: 'Node' : 'Ancestor' is not a valid template type argument for parameter 'T'
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(54): error C2065: 'Ancestor' : undeclared identifier
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(54): error C2923: 'Node' : 'Ancestor' is not a valid template type argument for parameter 'T'
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(100): error C2440: '=' : cannot convert from 'Node *' to 'Node<T> *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(101): error C2440: '=' : cannot convert from 'Node *' to 'Node<T> *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(105): error C2440: '=' : cannot convert from 'Node *' to 'Node<T> *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(109): error C2440: '=' : cannot convert from 'Node *' to 'Node<T> *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(132): error C2065: 'Ancestor' : undeclared identifier
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(132): error C2923: 'Node' : 'Ancestor' is not a valid template type argument for parameter 'T'
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(134): error C2065: 'Ancestor' : undeclared identifier
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(134): error C2923: 'Node' : 'Ancestor' is not a valid template type argument for parameter 'T'
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(137): error C2065: 'frontPtr' : undeclared identifier
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(137): error C2440: '=' : cannot convert from 'Node<T> *' to 'Node *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(139): error C2228: left of '.id' must have class/struct/union
1>          type is 'T'
1>c:\users\cameron\desktop\235\geneology - final project\level.cpp(7): error C2039: 'level' : is not a member of 'Geneology'
1>          c:\users\cameron\desktop\235\geneology - final project\geneologylist.h(19) : see declaration of 'Geneology'
1>  Generating Code...
1>  Skipping... (no relevant changes detected)
1>  main.cpp
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========






Last edited on
looks at your first error...
class Geneology : public List <Ancestor> // it doesn't know what Ancestor is.
geneologylist.h(19): error C2065: 'Ancestor' : undeclared identifier
Ancestor is not declared.
You need to include the file where you declared the class Ancestor in GeneologyList.h

geneologylist.h(22): error C2614: 'Geneology' : illegal member initialization: 'List' is not a base or member

In this error I also read something with initializing GeneologyList.h.
In your constructor, if you have an initializer list you need to initialize List<Ancestor>, not List

1
2
3
4
5
6
7
8
9
10
11
12
#include "List.h"
#include "Ancestor.h"

class Geneology : public List <Ancestor>
{
public:
    Geneology() 
//    : List( /* something */ ) // wrong!
      : List<Ancestor>( /* something */ ) // correct
    {}
	// Some Content Here	
};


geneologylist.h(137): error C2440: '=' : cannot convert from 'Node<T> *' to 'Node *'
Maybe you just forgot a <T> somewhere

1
2
Node* data; // wrong
Node<T>* data; // correct 
Last edited on
I think that's just some strange side effect of my regular problem. This is what the Geneological class actually looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   class Geneology : public List <Ancestor>
{
public:
	Geneology() : List <Ancestor>(), familyTreeRoot(NULL) {}
	void makeTree(const char * fileName);
	void displayTree();
	


        void level(); // Note that this is the function i'm defining in a separate file

	Node<Ancestor> * find(int id);

private:
	int getIntID(char * readFile);

	Node<Ancestor> * familyTreeRoot;
	
};



Again, by commenting out just this one #include, it will work perfectly.

From level.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

//#include "queue.h"
//#include "geneologylist.h" // uncommenting or commenting this will stop/solve the compilation


//void Geneology::level()
//{
  //const int MAX = 100;
  //BTree *queue[MAX];
  //BTree *temp;
  //int front = 0;
  //int back = 0;

  //queue[back++] = this;

  //while (front != back)
  //{
  //  temp = queue[front];
  //  front = (front + 1) % MAX;
  //  if (temp != NULL)
  //  {
  //    // visit
  //    cout.width(4);
  //    cout << temp->data << " ";
  //    // end Visit        
  //    queue[back] = temp->left;
  //    back = (back + 1) % MAX;
  //    queue[back] = temp->right;
  //    back = (back + 1) % MAX;

  //  }
  //}
//}


//#endif

Last edited on
Again, by commenting out just this one #include, it will work perfectly.
Does it really run if you don't include that ?
Did you add the include of Ancestor in geneologylist.h?

Edit: where do you implement find and getIntID?
Last edited on
The rule with getting things in order is: something must be known before it can be used.

1
2
// ancestor.hpp
class Ancestor { }
1
2
// geneology.hpp
class Geneology : public List <Ancestor> { }

You must #include "ancestor.hpp" at the top of geneology.hpp, otherwise the compiler will have no idea what an Ancestor is.


Something that is common (and useful) is to create typedefs in your classes:

1
2
3
4
5
6
7
8
class Geneology : public List <Ancestor>
{
public:
    typedef List <Ancestor> base_type;

    Geneology()
      : base_type( ... )
    {}

Hope this helps.
Hi guys I updated that code but it doesn't appear to do anything


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include "list.h"
#include <string>
#include <cassert>
#include <fstream>
#include <iostream>
#include <exception>
#include "ancestor.h"
using namespace std;


class Geneology : public List <Ancestor>
{
public:
	Geneology() : List <Ancestor>(), familyTreeRoot(NULL) {}
	void makeTree(const char * fileName);
	void displayTree();
	//void level();

	Node<Ancestor> * find(int id);

private:
	int getIntID(char * readFile);

	Node<Ancestor> * familyTreeRoot;
	
};
Hi guys I updated that code but it doesn't appear to do anything

Why did you comment out level now?
The errors should go away at least.
Last edited on
Maybe this could help?

Thank you so much for taking your time on this.

Here are the visual studio errors



Error	5	error LNK2005: "public: void __thiscall Geneology::makeTree(char const *)" (?makeTree@Geneology@@QAEXPBD@Z) already defined in main.obj	C:\Users\Cameron\documents\visual studio 2013\Projects\Geneology\Geneology\level.obj	Geneology

Error	10	error LNK2005: "public: void __thiscall Geneology::makeTree(char const *)" (?makeTree@Geneology@@QAEXPBD@Z) already defined in level.obj	C:\Users\Cameron\documents\visual studio 2013\Projects\Geneology\Geneology\main.obj	Geneology

Error	2	error LNK2005: "public: void __thiscall Geneology::displayTree(void)" (?displayTree@Geneology@@QAEXXZ) already defined in main.obj	C:\Users\Cameron\documents\visual studio 2013\Projects\Geneology\Geneology\level.obj	Geneology

Error	7	error LNK2005: "public: void __thiscall Geneology::displayTree(void)" (?displayTree@Geneology@@QAEXXZ) already defined in level.obj	C:\Users\Cameron\documents\visual studio 2013\Projects\Geneology\Geneology\main.obj	Geneology

Error	3	error LNK2005: "public: class Node<struct Ancestor> * __thiscall Geneology::find(int)" (?find@Geneology@@QAEPAV?$Node@UAncestor@@@@H@Z) already defined in main.obj	C:\Users\Cameron\documents\visual studio 2013\Projects\Geneology\Geneology\level.obj	Geneology

Error	8	error LNK2005: "public: class Node<struct Ancestor> * __thiscall Geneology::find(int)" (?find@Geneology@@QAEPAV?$Node@UAncestor@@@@H@Z) already defined in level.obj	C:\Users\Cameron\documents\visual studio 2013\Projects\Geneology\Geneology\main.obj	Geneology

Error	1	error LNK2005: "public: bool __thiscall Ancestor::operator<(struct Ancestor const &)" (??MAncestor@@QAE_NABU0@@Z) already defined in main.obj	C:\Users\Cameron\documents\visual studio 2013\Projects\Geneology\Geneology\level.obj	Geneology

Error	6	error LNK2005: "public: bool __thiscall Ancestor::operator<(struct Ancestor const &)" (??MAncestor@@QAE_NABU0@@Z) already defined in level.obj	C:\Users\Cameron\documents\visual studio 2013\Projects\Geneology\Geneology\main.obj	Geneology

Error	4	error LNK2005: "private: int __thiscall Geneology::getIntID(char *)" (?getIntID@Geneology@@AAEHPAD@Z) already defined in main.obj	C:\Users\Cameron\documents\visual studio 2013\Projects\Geneology\Geneology\level.obj	Geneology

Error	9	error LNK2005: "private: int __thiscall Geneology::getIntID(char *)" (?getIntID@Geneology@@AAEHPAD@Z) already defined in level.obj	C:\Users\Cameron\documents\visual studio 2013\Projects\Geneology\Geneology\main.obj	Geneology

Error	11	error LNK1169: one or more multiply defined symbols found	C:\Users\Cameron\documents\visual studio 2013\Projects\Geneology\Debug\Geneology.exe	1	1	Geneology



If I figure it out I will post the explanation too.
how does your main.cpp and level.cpp look like?

It looks like you implement the methods getIntID, makeTree, displayTree, find and operator< in both of them.

Did you by any chance include the level.cpp file somewhere instead of the level.h file?
I don't have a level.h file. Do I need one?


Here is the entire main file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using namespace std;
#include <string>
#include <iostream>
#include <fstream>
#include "stack.h"
#include "geneologylist.h"

void insertSortedly(Geneology & alphabetizedList, Ancestor & currentAncestor);
Geneology makeList(const char * fileName);


int main()
{
	Geneology FamilyOfGregCameron = makeList("C:\\Users\\Cameron\\Desktop\\235\\Geneology - Final Project\\cameronGED.txt");
	
	FamilyOfGregCameron.makeTree("C:\\Users\\Cameron\\Desktop\\235\\Geneology - Final Project\\cameronGED.txt");
	FamilyOfGregCameron.displayTree();

	char wait;
	cin >> wait;

	return 0;
}

Geneology makeList(const char * fileName)
{
	// Does something
}

void insertSortedly(Geneology & alphabetizedList, Ancestor & currentAncestor)
{
	// Does something
}





I did have an #include "level.cpp" in main but I got rid of it. I'm not sure I'm the best with the #includes. Thanks for replying back
Last edited on
I appreciate your help. However, I might just put the level function into the "geneologylist.h" as I know this will work. If you see something though please let me know because I find it frustrating not to know why this won't work.
I appreciate your help. However, I might just put the level function into the "geneologylist.h" as I know this will work. If you see something though please let me know because I find it frustrating not to know why this won't work.
if you do this make sure to declare the function to be inline because of the one-definition-rule

I don't have a level.h file. Do I need one?

oh no sorry, of course not as long as you have your geneologylist.h file with the decleration.

I did have an #include "level.cpp" in main but I got rid of it.
that should have solved these problems.



Usually you should split a class in 2 parts, 1 *.hpp file with the declerations, 1 *.cpp file with the implementations.
You MUST NOT include the *.cpp file because you'll compile the functions in the *.cpp file twice and so you have the same functions in 2 *.obj files and therefore multiple definitions of the function.
Also, get rid of that namespace using stuff in header files.

Here's an overview of using multiple sources that may help:
http://www.cplusplus.com/faq/beginners/multiple-sources/
Also, get rid of that namespace using stuff in header files.


Will do.

that should have solved these problems.


It solved one of the problems, but those outputs shown are the outputs shown after removing the include level.cpp.

if you do this make sure to declare the function to be inline because of the one-definition-rule


Maybe this is bad but couldn't I just have the function defined inside the header files and not in any cpp files? That is what I have been doing for the vast majority of my code. It makes compilations much easier.
Last edited on
Maybe this is bad but couldn't I just have the function defined inside the header files and not in any cpp files? That is what I have been doing for the vast majority of my code. It makes compilations much easier.

It makes it easier as long as you only have your main.cpp file and no others at all.

As I said, if you want to do this you should mark the function inline, alternatively you could just learn how to split your decleration (*.hpp) from your implementation (*.cpp).
Topic archived. No new replies allowed.