Help, can't get rid of simple errors.
Feb 1, 2014 at 8:41am UTC
I've been working on this program for a few days now, and I'm just stumped at this point on how to go about fixing my errors, it has to do with the constructor but I just can't see the fix.
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
int main()
{
CName nameOne("Robert" , "Bresson" );
const CName nameTwo = nameOne;
CName nameThree;
cout << "nameOne = " ;
nameOne.WriteFullName();
cout << endl;
cout << "nameTwo = " ;
nameTwo.WriteFullName();
cout << endl;
cout << "nameThree = " ;
nameThree.WriteFullName();
cout << endl;
cout << "Enter your first and last names separated by a space: " ;
cin >> nameThree;
cout << "Your name is: " << nameThree << endl << endl;
nameOne = nameThree = nameTwo;
cout << "Testing the assignment operator..." << endl;
cout << "nameOne == " << nameOne << endl;
cout << "nameTwo == " << nameTwo << endl;
cout << "nameThree == " << nameThree << endl;
};
I'm not allowed to edit the above code for the assignment.
1 2 3 4 5 6 7
class CName
{
public :
CName();
CName(const string, const string);
void WriteFullName() const ;
The above code is my .h file
and the following code is where I am assuming my error lies.
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
#include "cname.h"
#include <iostream>
CName::CName()
{
};
CName::CName(const string first,const string last)
{
strncpy(m_first, first.c_str(), sizeof (m_first));
m_first[sizeof (m_first) - 1] = 0;
strncpy(m_last, last.c_str(), sizeof (m_last));
m_last[sizeof (m_last) - 1] = 0;
};
void CName::WriteFullName() const
{
cout << m_first << ' ' << m_last;
};
the error I am receiving is the following.
main.cpp:(.text+0x22d): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, CName&)'
main.cpp:(.text+0x251): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x2ee): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x325): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x359): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x41a): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x441): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, CName&)'
any help would be greatly appreciated.
Feb 1, 2014 at 8:59am UTC
I think that as part of the assignment you need to overload the stream input/output operators, as they are used in the
main function. The errors you are getting are showing that the functions have been declared, but have not been implemented anywhere. This is what you should have somewhere (probably with different variable names):
1 2
std::istream& operator >> (std::istream& in, CName& obj);
std::ostream& operator << (std::ostream& out, const CName& obj);
And your
CName class should have these lines, too:
1 2
friend std::istream& operator >> (std::istream&, CName&);
friend std::ostream& operator << (std::ostream&, const CName&);
Basically, you need to implement those functions. If you don't know how, look up 'overloading the i/o operators' or something like that.
Feb 1, 2014 at 9:15am UTC
there is indeed a:
1 2
istream& operator >> (istream& in, CName& obj);
ostream& operator << (ostream& out, const CName& obj);
at the bottom of the cname.h file. I guess I have been looking at the code so long I forgot it was there. Thank you for the virtual whack on the head. Seriously.
Topic archived. No new replies allowed.