keyeditem.h problem

Hello, I've ran into a problem with my header file. I can't figure out what's wrong with my keyedItem class. It looks like keyedItem is the main problem for not my program. Any suggestions? Thanks for reading.

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

class keyedItem
{
public:
keyedItem(){}
keyedItem(const string &keyValue,string &fName, string &idNum, int &s1, int &s2)
{
setLastName(keyValue);
setFirstName(fName);
setID(idNum);
setScore1(s1);
setScore2(s2);
}
void setLastName(string lName)
{
lastName=lName;
}
void setFirstName(string fName)
{
firstName=fName;
}
void setID(string idNum)
{
ID=idNum;
}
void setScore1(int s1)
{
score1=s1;
}
void setScore2(int s2)
{
score2=s2;
}
string getKey()
{
return lastName;
}
string getFirstName()
{
return firstName;
}
string getID()
{
return ID;
}
int getScore1()
{
return score1;
}
int getScore2()
{
return score2;
}
void displayRoster()
{
cout<<getKey()<<" "<<getFirstName()<<" "<<getID();
cout<<endl;
}
void gradeReport()
{
cout<<getID()<<" "<<getScore1()<<" "<<getScore2();
cout<<endl;
}
private:
string lastName;
string firstName;
string ID;
int score1;
int score2;
};

Here is the output:

1>------ Build started: Project: final, Configuration: Debug Win32 ------
1> gradeBookTest.cpp
1>e:\csci41\assignments\final\keyeditem.h(7): error C2011: 'keyedItem' : 'class' type redefinition
1> e:\csci41\assignments\final\keyeditem.h(7) : see declaration of 'keyedItem'
1>e:\csci41\assignments\final\treenode.h(15): error C2079: 'treeNode::item' uses undefined class 'keyedItem'
1>e:\csci41\assignments\final\treenode.h(10): error C2440: '=' : cannot convert from 'const keyedItem' to 'int'
1> Source or target has incomplete type
1> binarySearchTree.cpp
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Usually for C/C++ header file we have include guard to prevent the same .h file being included multiple times leading to those multiply defined problem when we have a C++ project consisting of a lot of files.

Try use below on all your .h file

E.g

#ifndef KEYEDITEM
#define KEYEDITEM

class keyedItem {
...

};
#endif
sweet jesus! , awesome, it works I'm not really sure why it works though, but thanks a lot...but I've ran into another problem =/ my insertItem function is not working.


void binarySearchTree::insertItem(treeNode *&treePtr,const keyedItem &newItem)
{
if(treePtr==NULL)
treePtr=new treeNode(newItem,NULL,NULL);
else if(newItem.getKey()<treePtr->item.getKey()) //newItem.getKey() doesn't seem to work
insertItem(treePtr->left,newItem);
else
insertItem(treePtr->right,newItem);

}


output:
1>------ Build started: Project: final, Configuration: Debug Win32 ------
1> binarySearchTree.cpp
1>e:\csci41\assignments\final\binarysearchtree.cpp(31): error C2662: 'keyedItem::getKey' : cannot convert 'this' pointer from 'const keyedItem' to 'keyedItem &'
1> Conversion loses qualifiers
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I think the error message is quite clear.

You declare "const keyedItem &newItem"

Then you define newItem.getKey() < treePtr->item.getKey() which seems to compare const keyedItem& against keyedItem&

Simply change your declaration to keyedItem &newItem should do fine.
haha nice, yeah i thought so, sorry bad source, and book was no help, your awesome
Last edited on
Please do not bring up "teacher's code" etc in this forum. Previously a guy also post his teacher sample code and it was being criticised as not conforming to standard C++. In the end he was asked to take his post down and "reprimanded" by his teacher.

Please note teacher do surf Internet too. If you want help, please leave "teacher's code" etc this phrase out of your posting else soon you will get no help and a fail grade even!
cool, thanks for looking out
Topic archived. No new replies allowed.