Sign in to follow this  
striderx240

keyedItem.h problem

Recommended Posts

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 ==========

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ==========

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this