将向量<string>作为函数参数传递时出现问题 C++
Trouble passing vector<string> as function parameter C++
这就是我正在使用的代码。我希望能够将所有输入传递到此函数中,new_flight
该函数目前没有其他代码,而是空声明。我正在尝试通过引用传递令牌,但我已经尝试过*
&
并且仅按值传递令牌,但似乎都没有工作。
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
void new_flight( vector<string> &tokens );
int main( int argc, char *argv[] )
{
vector<string> tokens;
cout << "Reservations >> ";
getline(cin, input);
istringstream iss( input );
copy(istream_iterator<string>( iss ),
istream_iterator<string>(),
back_inserter<vector<string> > ( tokens ));
new_flight( tokens );
}
这是编译器告诉我的
Undefined symbols for architecture x86_64:
"new_flight(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)", referenced from:
_main in ccplPBEo.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
此外,如果我注释掉我实际将令牌传递给new_flight new_flight( tokens )
的行,它可以很好地编译。
感谢您的观看
为了存根一个函数,你需要提供一个函数定义,而不是一个函数声明:
void new_flight( vector<string> &tokens ) {
// Not implemented
}
你得到的不是编译器错误,而是链接器错误,这是由于你的函数new_flight()
没有定义。但你似乎意识到了这个事实。如果调用未定义的函数,则不能期望程序正常工作,因此链接器首先拒绝创建它。
您正在声明函数new_flight
,但没有定义它,因此链接器无法链接它。 编写实现(如果只是一个存根),它将编译。
不能调用声明。你需要一个定义。编译器应该为此调用生成什么代码?它没有函数的代码。无法编译程序。
正如其他帖子所指出的:
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
void new_flight( vector<string> &tokens );
int main( int argc, char *argv[] )
{
vector<string> tokens;
cout << "Reservations >> ";
getline(cin, input);
istringstream iss( input );
copy(istream_iterator<string>( iss ),
istream_iterator<string>(),
back_inserter<vector<string> > ( tokens ));
new_flight( tokens );
}
void new_flight( vector<string> &tokens )
{
// implementation
}
因为你实际上是在 main 之后定义功能,所以编译器需要知道函数的存在,因此我们创建了一个定义函数的"原型"void new_flight( vector<string> &tokens );
。
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用指向成员的指针将成员函数作为参数传递
- 如何将参数传递给正在使用模板的类
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将部分流作为参数传递
- 如何修复带有 clang 的参数'args'缺少默认参数的问题?
- 我正在开发服务器,ip作为参数传递不起作用
- 错误:如何通过参数传递来解决此问题
- 将数组作为参数传递时出现问题
- 将对象作为参数传递时出现问题
- POSIX线程同步和/或pthread_create()参数传递问题
- C++中传递引用中函数参数的问题
- 将常量引用对象作为参数传递时出现问题,VS 调试器报告"'.getXCoord'左侧必须具有调用/结构/联合"
- 此互操作 C#/托管/非托管C++参数传递有什么问题?
- 通过指针引用传递参数的问题
- 参数包传递问题
- 将向量<string>作为函数参数传递时出现问题 C++
- C++编译器问题 (?):无法将参数传递给单独类文件中的函数
- 将std::function作为参数传递时出现问题