'arrayName'之前的预期primary-expression

时间:2016-04-26 19:09:52

标签: c++

我不明白为什么会出现这种预期的主要异常错误! ......:

Running /home/ubuntu/workspace/sample.cpp
/home/ubuntu/workspace/sample.cpp: In function ‘int main()’:
/home/ubuntu/workspace/sample.cpp:50:22: error: expected primary-expression before ‘arrayHelpfull’
          std::string arrayHelpfull[3],
                      ^
/home/ubuntu/workspace/sample.cpp:51:10: error: expected primary-expression before ‘double’
          double arrayHelpfullPoints[3],
          ^
/home/ubuntu/workspace/sample.cpp:52:22: error: expected primary-expression before ‘arrayNone’
          std::string arrayNone[3],
                      ^
/home/ubuntu/workspace/sample.cpp:53:10: error: expected primary-expression before ‘double’
          double arrayNonePoints[3],
          ^
/home/ubuntu/workspace/sample.cpp:54:23: error: expected primary-expression before ‘arrayHarmfull’
           std::string arrayHarmfull[3],
                       ^
/home/ubuntu/workspace/sample.cpp:55:11: error: expected primary-expression before ‘double’
           double arrayHarmfullPoints[3],
           ^
/home/ubuntu/workspace/sample.cpp:61:20: error: expected primary-expression before ‘arrayHelpfull’
        std::string arrayHelpfull[3],
                    ^
/home/ubuntu/workspace/sample.cpp:62:8: error: expected primary-expression before ‘double’
        double arrayHelpfullPoints[3],
        ^
/home/ubuntu/workspace/sample.cpp:63:20: error: expected primary-expression before ‘arrayNone’
        std::string arrayNone[3],
                    ^
/home/ubuntu/workspace/sample.cpp:64:8: error: expected primary-expression before ‘double’
        double arrayNonePoints[3],
        ^
/home/ubuntu/workspace/sample.cpp:65:21: error: expected primary-expression before ‘arrayHarmfull’
         std::string arrayHarmfull[3],
                     ^
/home/ubuntu/workspace/sample.cpp:66:9: error: expected primary-expression before ‘double’
         double arrayHarmfullPoints[3],
         ^
/home/ubuntu/workspace/sample.cpp:74:20: error: expected primary-expression before ‘arrayHelpfull’
        std::string arrayHelpfull[3],
                    ^
/home/ubuntu/workspace/sample.cpp:75:8: error: expected primary-expression before ‘double’
        double arrayHelpfullPoints[3],
        ^
/home/ubuntu/workspace/sample.cpp:76:20: error: expected primary-expression before ‘arrayNone’
        std::string arrayNone[3],
                    ^
/home/ubuntu/workspace/sample.cpp:77:8: error: expected primary-expression before ‘double’
        double arrayNonePoints[3],
        ^
/home/ubuntu/workspace/sample.cpp:78:21: error: expected primary-expression before ‘arrayHarmfull’
         std::string arrayHarmfull[3],
                     ^
/home/ubuntu/workspace/sample.cpp:79:9: error: expected primary-expression before ‘double’
         double arrayHarmfullPoints[3],
         ^

代码:

#include<iostream>
#include<string>
#include<fstream>


void printingItems(


    std::string arrayHelpfull[3],
    double arrayHelpfullPoints[3],
    std::string arrayNone[3],
    double arrayNonePoints[3],
     std::string arrayHarmfull[3],
     double arrayHarmfullPoints[3],
    std::string option

    ) {
    std::cout << option << std::endl;

}


int main(){
  std::string arrayHelpfull[3] = {"fruits", "soda" , "candy"};
  double arrayHelpfullPoints[3] = {20.4,50.2,30.0};

    std::string arrayNone[3] = {"chair", "shoe" , "pencil"};
  double arrayNonePoints[3] = {0,0,0};

    std::string arrayHarmfull[3] = {"meth", "dirtyneedle","ninga"};
  double arrayHarmfullPoints[3] = {-20,-50,-30};

  int userChoice = 0;
    while (userChoice != 4) {

      std::cout << "1 - Just Plain Items"
            << std::endl
            << "2 - Helpfull Items"
            << std::endl
            << "3 - Harmfull Items"
            << std::endl
            << "4 - Quit"
            << std::endl;

        std::cin >> userChoice;

        switch (userChoice) {
        case 1:
            printingItems(
            std::string arrayHelpfull[3],
            double arrayHelpfullPoints[3],
            std::string arrayNone[3],
            double arrayNonePoints[3],
             std::string arrayHarmfull[3],
             double arrayHarmfullPoints[3],
            "Plain"
         );
            break;
        case 2:
        printingItems(
        std::string arrayHelpfull[3],
        double arrayHelpfullPoints[3],
        std::string arrayNone[3],
        double arrayNonePoints[3],
         std::string arrayHarmfull[3],
         double arrayHarmfullPoints[3],
        "Helpfull"
        );

            break;
        case 3:

        printingItems(
        std::string arrayHelpfull[3],
        double arrayHelpfullPoints[3],
        std::string arrayNone[3],
        double arrayNonePoints[3],
         std::string arrayHarmfull[3],
         double arrayHarmfullPoints[3],
        "Harmfull"
        );

            break;
        }


    }
}

1 个答案:

答案 0 :(得分:1)

printingItems(
            std::string arrayHelpfull[3],
            double arrayHelpfullPoints[3],
            std::string arrayNone[3],
            double arrayNonePoints[3],
             std::string arrayHarmfull[3],
             double arrayHarmfullPoints[3],
            "Plain"
         );

不是如何调用函数并将变量传递给它。您只需要将该变量的名称赋予该函数。你正在做的是试图在函数调用中重新声明变量。您的所有函数调用应该看起来像

printingItems(arrayHelpfull, arrayHelpfullPoints, arrayNone,
              arrayNonePoints, arrayHarmfull, arrayHarmfullPoints, "Plain");

您还需要使用一些一致的缩进。缩进后,更改会使代码难以阅读。