2

I'm having trouble with passing a constant class through a function.

 // test the constructors
    auto    CName       nameOne("Robert", "Bresson");
    const   CName       nameTwo = nameOne;
    auto    CName       nameThree;

    // display the contents of each newly-constructed object...

    // should see "Robert Bresson"
    cout << "nameOne = ";
    nameOne.WriteFullName();
    cout << endl;

    // should see "Robert Bresson" again
    cout << "nameTwo = ";
    nameTwo.WriteFullName();
    cout << endl;

As soon as the compiler hits nameTwo.WriteFullName() I get the error of abandoning qualifiers. I know that the class is a constant however I can't figure out how to work around it.

The function is in a header file written as so:

void    const WriteFullName(ostream& outstream = cout)
{
    outstream << m_first << ' ' << m_last;
}

I receive this error when const is put in back of the function header

main.cpp:(.text+0x51): undefined reference to `CName::CName()'
main.cpp:(.text+0x7c): undefined reference to `CName::WriteFullName(std::basic_ostream<char, std::char_traits<char> >&) const'
main.cpp:(.text+0xbb): undefined reference to `CName::WriteFullName(std::basic_ostream<char, std::char_traits<char> >&) const'
main.cpp:(.text+0xf7): undefined reference to `CName::WriteFullName(std::basic_ostream<char, std::char_traits<char> >&) const'
main.cpp:(.text+0x133): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, CName&)'
main.cpp:(.text+0x157): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x1f4): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x22b): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x25f): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x320): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x347): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, CName&)'
bstpierre
  • 30,042
  • 15
  • 70
  • 103
Geno Diaz
  • 400
  • 1
  • 7
  • 21
  • 1
    A small and unrelated tip: Don't use `auto` the way you do. In the old C++03 standard it really doesn't do anything, and in the new C++11 standard it is used to infer the types of the variables. – Some programmer dude Sep 07 '12 at 09:34
  • Ahh thanks, its an old habit my teacher instilled on us. Auto was used to automatically create and discard at the end of programs right? – Geno Diaz Sep 07 '12 at 09:37
  • To make function `const` you must supply keyword AFTER function name and signature: `void WriteFullName(ostream& outstream = cout) const { /* your code */}` – Rost Sep 07 '12 at 09:41
  • Yes, basically that's it. See [this old answer](http://stackoverflow.com/a/1046503/440558) for a good description. – Some programmer dude Sep 07 '12 at 09:44
  • @GenoDiaz: Your teacher's bad habit is no longer harmless. `auto` used to mean that the variable would be destroyed when it went out of scope, exactly as it would without `auto`, but in C++11 its meaning has changed so that your code will no longer compile. It's now a type specifier, declaring that a variable has the same type as its initialiser; so `auto nameFour = nameThree;` will give `nameFour` the type `CName`, to match `nameThree`. – Mike Seymour Sep 07 '12 at 10:52

1 Answers1

0

What you have is linker errors, which means that you don't link with all source files. I'm guessing that the code for the CName class is in a separate source file, and you forgot to build/link with it.

Edit: Compiling and linking multiple files

To make a program from more than one source file, you have to pass all the source files on the command line, like this:

$ g++ main.cpp cname.cpp -o myprogram

The above command uses both source files, and the -o option tells the compiler what to name the program.

You can also compile each source file separately and then link them together:

$ g++ main.cpp -c -o main.o
$ g++ cname.cpp -c -o cname.o
$ g++ main.o cname.o -o myprogram

The -c option tells the compiler to create an object file, that can later be used to link your program. The last command will not actually compile any sources, but instead uses the two object files to create the program.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I'm using putty with all files in the same directory. I am using main.cpp cname.h and cname.cpp for implementations should I post the complete header and implementations? – Geno Diaz Sep 07 '12 at 09:43
  • @GenoDiaz That shouldn't be needed, just the commands you use to compile and link all files. – Some programmer dude Sep 07 '12 at 09:45
  • Unfortunately when i build them all together my implementation file causes even more errors. such as scope definition error in my member functions trying to access other members. – Geno Diaz Sep 08 '12 at 08:37