我在通过函数传递常量类时遇到问题。
// 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;
一旦编译器点击 nameTwo.WriteFullName() 我就会得到放弃限定符的错误。我知道这个类是一个常数,但是我不知道如何解决它。
该函数在一个头文件中,如下所示:
void const WriteFullName(ostream& outstream = cout)
{
outstream << m_first << ' ' << m_last;
}
当将 const 放在函数头的后面时,我收到此错误
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&)'