5

我一直坚持这个,我的老师甚至不知道发生了什么。如果有人可以请帮助我,将不胜感激。

我已经在 Line 结构的头文件中声明了 item。但是,在 Line::display() 方法中调用它时,我收到一条错误消息,指出该变量未在范围内声明。我已经向我的老师和我的同龄人展示过,但似乎没有人知道解决方案。

这是我的.h:

//Line.h
#define MAX_CHARS 40
struct Line {
    public:
    bool set(int n, const char* str);
    void display() const;

    private:
    char item[MAX_CHARS];
    int no;
    };

这是我的 .cpp 文件。

// Line.cpp
#include "Line.h"
#include <iostream>
using namespace std;

#define MAX_CHARS 40

void Line::display() const {
    cout << no << ' ' << item << endl;
}

对此的任何帮助都很棒。提前致谢。

4

2 回答 2

6

如果这是您的实际代码,您可能是从其他地方获取标题。尝试:

#include "C:\\fullPathToHeader\\Line.h"
#include <iostream>
using namespace std;

void Line::display() const {
    cout << no << ' ' << item << endl;
}

还:

  • 不要MAX_CHARScpp文件中重新定义。
  • 对标题使用包含防护。
于 2012-06-05T15:38:10.483 回答
-5

要使 Line::display const 意味着您不需要实例变量数据。

// Line.cpp
#include "Line.h"
#include <iostream>
using namespace std;

void Line::display()
{
    cout << no << ' ' << Line::item << endl;
}
于 2012-06-05T15:39:24.040 回答