keyedItem.h problem

Started by
3 comments, last by striderx240 13 years, 4 months ago
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 ==========
Advertisement
Wrap your header file in something like:
#ifndef KEYEDITEM_H#define KEYEDITEM_H// ... Header Contents Here ...#endif
What's happening is every time you try to include this header file in another file, you end up redefining it. You need to make sure it only is defined once, using a method like the above.
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 ==========
The compiler complains, because you're using a const reference to a keyedItem, but the method getKey is obviously not marked as const.
Therefore the compiler thinks getKey will modify your const keyedItem instance and does not allow it.

Solution: Mark the method getKey as const.

string getKey() const{  return lastName;}

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

sweet, thanks, I knew I was missing something. You guys are awesome.

This topic is closed to new replies.

Advertisement