#ifndef GENEOLOGY_LIST_H
#define GENEOLOGY_LIST_H
#include "list.h"
#include <string>
#include <cassert>
#include <fstream>
#include <iostream>
#include <exception>
usingnamespace 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.
#ifndef ANCESTOR_H
#define ANCESTOR_H
usingnamespace 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;
}
booloperator <(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 ==========
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 *'
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(constchar * 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.
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.
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.
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).