简体   繁体   English

C++:预期的主表达式

[英]c++: Expected primary-expression

I have been struggling with a certain error that doesn't make sense to me.我一直在为一个对我没有意义的错误而苦苦挣扎。 Whenever I try to compile this program, it tells me that I'm missing a semicolon when I am not.每当我尝试编译此程序时,它都会告诉我缺少分号,而实际上却没有。

It seems the error is linked to a specific block of code, that being the if statement that checks stock.该错误似乎与特定的代码块有关,即检查库存的 if 语句。 Since I know c++ can be platform specific, I'm running debian 9 and the atom ide if that's any help.由于我知道 c++ 可以是特定于平台的,因此我正在运行 debian 9 和 atom ide(如果有帮助的话)。

Here is the specifc error:这是特定的错误:

error: expected primary-expression before ',' token错误:',' 标记前的预期主表达式

getline(string,line);//gets string` getline(string,line);//获取字符串`

and the code:和代码:

#include <iostream>
#include <fstream>


using namespace std;

int main ()
{
    cout << "store stocking system: \n"; // yadda yadda yadda UX
    cout << "commands: \n";
    cout << "  help: shows available commands\n  check stock: checks store stock\n  enter stock: enter new stock items\n";
    cout << "  exit: terminates the program\n  clean house: deletes all stock\n";
    home: // main loop in program
    string output;
    output = ">> ";
    cout << output;


    string stock;
    string item; // this whole block just defines things and gets input
    int itemNumber;
    string userInput;
    getline(cin,userInput);

    if (userInput == "exit")
    {
      return 0;
    }

    if (userInput == "enter stock")
    { // enters new stock
      cout << "enter item\n>> "; //item name
      cin >> item;
      cout << "enter amount\n>> "; //amount of item
      cin >> itemNumber;
      ofstream myfile; //makes file
      myfile.open("stock.txt"); //opens myfile
      myfile << "\n" << item << "," << itemNumber << "\n"; //writes to file
      myfile.close();// closes file
      cout << "done";
      goto home; //finishes and goes to main loop
    }

    if (userInput == "check stock") // where the problem is
    {
      string line;
      ifstream file("stock.txt");//checks fo file
      file.open("stock.txt");//opens file
      getline(string,line);//gets string
      file.close();//closes it
      cout << line << "\n";
      goto home;
    }

    if (userInput == ""){
      goto home;
    }

    else
    {
      cout << "\033[1;31mplease use a proper command:\033[0m\n";
      goto home;

    }
    return 0;
}    

你有没有机会错过这个?

#include <string>

我相信它只需要getline(file,line)而不是getline(string,line)然后你应该被排序。

string is recognized as a type name, of type std::string which you generously exposed by line using namespace std; string被识别为类型名称,属于std::string类型,您可以using namespace std;通过行慷慨地公开它using namespace std; . . This particular error message is caused by fact that string isn't an expression which can be evaluated .此特定错误消息是由string不是可以计算的表达式这一事实引起的。 In context of your code it should be在您的代码上下文中,它应该是

getline(file,line);

PS.附注。 Standard would say that you have to include <string> header to use component std::string .标准会说您必须包含<string>标头才能使用组件std::string Your code compiles thanks to an implementation-defined feature, was imported with <iostream> in this version of header.由于实现定义的功能,您的代码编译,在此版本的头文件中使用<iostream>导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM