重定向 C++溪流
我有一个方法搜索,我想提供输入流,然后将输出放在输出流上。
void search(std::istream & is, std::ostream & os);
现在我想用 cin/cout 来做这件事。从命令提示符处:
a.out < input_file.txt
在 main 中,我尝试通过将 cin/cout 传递给 search() 方法来执行此操作。
X.search(std::cin, std::cout);
编译和链接(XCode)时出现以下错误:
Line Location Tool:0: collect2: ld returned 1 exit status
Line Location Tool:0: symbol(s) not found
Line Location Tool:0: _main in main.o
Line Location Tool:0: "X::search(std::basic_istream<char, std::char_traits<char> >&,
std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
我需要为 cin/cout 做些什么不同的事情吗?我不知道如何解决这个错误。
I have a method search that I want to feed an input stream and then put output on an output stream.
void search(std::istream & is, std::ostream & os);
For now I'd like to do this with cin/cout. From the command prompt:
a.out < input_file.txt
In main, I try to do this by passing cin/cout to the search() method.
X.search(std::cin, std::cout);
I get the following error when I compile and link (XCode):
Line Location Tool:0: collect2: ld returned 1 exit status
Line Location Tool:0: symbol(s) not found
Line Location Tool:0: _main in main.o
Line Location Tool:0: "X::search(std::basic_istream<char, std::char_traits<char> >&,
std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
Is there anything different I need to be doing for cin/cout? I cannot figure out how to resolve this error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在 xcode 中编译并运行良好(c++ cmdline 新项目向导),看起来您缺少 #include;或 main,或 X 类的定义。
This compiles and runs fine for me in xcode (c++ cmdline new project wizard), looks like you're missing an #include <iostream> or main, or a definition of your X class.
我不相信你的问题与 std::cin / std::cout 有关。
从给出的信息中很难猜测,但我认为问题在于搜索函数的定义没有被编译/链接到您的可执行文件中。您还可以包含更多编译器输出吗?
I do not believe your problem is related to std::cin / std::cout.
It's hard to guess from the information given, but I think the problem is that the definition of the search function is not being compiled/linked into your executable. Is there any more compiler output you can include?