c++ - C++编译器错误: 'not declared in this scope'

标签 c++ compiler-errors

我的程序检查有多少学生通过/未通过考试。

我以为我以前已经声明了i,但是编译器仍然显示以下错误:

 "[Error] 'i' was not declared in this scope"

此外,如果我将其更改为:l.checkPass(i),则grade_test.cpp中包含:l.checkPass(int i)的行会标记为红色,编译器会说:
 [Error] expected primary-expression before 'int'  

这是我的代码:
grade.h:
class Grade
{
    int mid_term, final;
    int total;  

    public:
        int i;
        Grade *next;
        Grade();
        Grade(int i_mid_term, int i_final, int i_total);
        void readFile(string _file);
        void printList();
        void subString(string s);
        int Show();
        void addTail(Grade *q);
        int checkPass(int i);
};

#endif
grade.cpp:
int Grade::Show()
    {
        cout << mid_term << "-" << final << "-" << total << endl;
        if (mid_term < 4 || final < 4 || total < 10)
        {
            cout << "fail" << endl;
            i = i;
        }
        else 
        {
            cout << "pass" << endl;
            i++;
        }
        return i;
    }

int Grade::checkPass(int i)
    {
        cout << i << " student passed." << endl;
        cout << 6-i << " student failed." << endl; 
    } 
grade_test.cpp:
int main()
{
    Grade l;
    l.readFile("mark.txt");
    l.printList(); 
    l.checkPass(i); 
    system("pause");
    return 0;
}

题:

错误的原因可能是什么?

最佳答案

您不需要i作为Grade::checkPass()的参数。

尝试像这样更改文件:

等级h

class Grade
{
    int mid_term, final;
    int total;  

    public:
        int i;
        Grade *next;
        Grade();
        Grade(int i_mid_term, int i_final, int i_total);
        void readFile(string _file);
        void printList();
        void subString(string s);
        int Show();
        void addTail(Grade *q);
        int checkPass(); // remove "int i"
};

#endif

等级
int Grade::Show()
    {
        cout << mid_term << "-" << final << "-" << total << endl;
        if (mid_term < 4 || final < 4 || total < 10)
        {
            cout << "fail" << endl;
            // i = i; // not harmful but do nothing
        }
        else 
        {
            cout << "pass" << endl;
            i++;
        }
        return i;
    }

int Grade::checkPass() // remove "int i" so that it will see the member variable i
    {
        cout << i << " student passed." << endl;
        cout << 6-i << " student failed." << endl; 
    } 

grade_test.cpp
int main()
{
    Grade l;
    l.readFile("mark.txt");
    l.printList(); 
    l.checkPass();  // remove "i"
    system("pause");
    return 0;
}

关于c++ - C++编译器错误: 'not declared in this scope' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32797827/

相关文章:

c++ - 在 Ubuntu 上使用 Clion 进行 lncurses

java - 我无法使用删除(int)方法,符号未找到错误

c++ - g++ 使用模板时出现重复符号错误(菜鸟问题)

asp.net - System.NullReferenceException : Object reference not set to an instance of an object. in vb.net

c++ - 登录到 Tizen 中的特定文件?

c++ - 考虑到智能指针以及函数 std::make_shared 和 std::make_unique ,关键字 new 及其对应的 delete 是否已经过时?

C++:为什么引入 std::vector::data 成员函数?

java - 数组中的 GUI 按钮

c++ - 无法将C共享库链接到C++程序

c++ - error LNK2019 函数中引用的未解析外部符号dbbind