gpt4 book ai didi

c++ - 为什么我会在意外标记附近收到语法错误?命令行参数?

转载 作者:搜寻专家 更新时间:2023-10-31 00:40:27 24 4
gpt4 key购买 nike

这是我的代码 - 不确定为什么会收到此错误消息:

$ ./main.cpp "hello" "is"
./main.cpp: line 4: syntax error near unexpected token `('
./main.cpp: line 4: `int main(int argc, char *argv[]){'

它在 g++ 中编译得很好,但是当我运行它时,我得到了上面的错误。知道为什么吗?这是我的完整代码..

#include <iostream>
#include <fstream>

int main(int argc, char *argv[]){

for(int i = 0; i < argc; i++){
std::cout << argc << " : " << argv[i] << '\n';
}

if (argc != 2){
std::cout << "\nUSAGE: 2 command line arguments please." << std::endl;
std::cout << "\n (1) Input file with raw event scores.\n (2) Output file to write into.";
}

// open the font file for reading
std::string in_file = argv[1];
std::ifstream istr(in_file.c_str());
if (!istr) {
std::cerr << "ERROR: Cannot open input file " << in_file << std::endl;
}

return 0;
}

最佳答案

您必须运行编译后的程序,而不是源代码:

$ g++ -o main main.cpp
$ ./main "hello" "is"
3 : ./main
3 : hello
3 : is

USAGE: 2 command line arguments please.

   (1) Input file with raw event scores.
   (2) Output file to write into.ERROR: Cannot open input file hello

您的示例试图将 C++ 代码作为 shell 脚本执行,这是行不通的。正如您在此处从我的程序测试运行输出中看到的那样,您仍然有一些错误需要解决。

关于c++ - 为什么我会在意外标记附近收到语法错误?命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14736523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com