我不确定为什么会收到此错误。该函数应返回true
或false
。
所有变量都已定义,我得到的错误是:
/tmp/ccTspEHr.o: In function `main':
rectangles.cpp:(.text+0x169): undefined reference to `rectangle_name(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<Rectangle, std::allocator<Rectangle> >)'
rectangles.cpp:(.text+0x310): undefined reference to `rectangle_name(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<Rectangle, std::allocator<Rectangle> >)'
rectangles.cpp:(.text+0x7b0): undefined reference to `rectangle_name(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<Rectangle, std::allocator<Rectangle> >)'
collect2: ld returned 1 exit status
功能原型:
bool rectangle_name(string prompt, string invalid, string used, string n, vector<Rectangle> rectangle);
功能定义:
bool rectangle_name(string prompt, string invalid, string used, string & n, vector<Rectangle> rectangle)
//This function reads in a prompt and reads in the name of the rectangle or stop\
{
cout << prompt;
getline (cin, n);
// do other stuff and return true or false
}
导致这些错误的原因是什么?
答案 0 :(得分:2)
你宣布了
bool rectangle_name(string prompt, string invalid, string used, string n, vector<Rectangle> rectangle);
^^^^^^^
并定义了
bool rectangle_name(string prompt, string invalid, string used, string & n, vector<Rectangle> rectangle)
^^^^^^^
看第四个参数?声明string n
和定义string & n
。他们不匹配。使这两个匹配,错误将消失。
答案 1 :(得分:2)
很简单。你应该定义这个功能。您将该函数声明为
bool rectangle_name(string prompt, string invalid, string used, string n, vector<Rectangle> rectangle);
但定义了另一个功能
bool rectangle_name(string prompt, string invalid, string used, string & n, vector<Rectangle> rectangle)
注意名称为'n'的参数声明。