Not declared in scope, although declared in .h

I am stuck on this, my teacher does not even know what is happening. If someone could help me, that would be very grateful.

I declared an element in the header file in the line structure. However, when you call it in the Line :: display () method, I get an error message indicating that the variable was not declared in the scope. I showed my teacher and my peers, and no one knows about the solutions.

Here is my .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; }; 

And here is my .cpp file.

 // Line.cpp #include "Line.h" #include <iostream> using namespace std; #define MAX_CHARS 40 void Line::display() const { cout << no << ' ' << item << endl; } 

Any help with this is wonderful. Thanks in advance.

+4
source share
2 answers

If this is your actual code, you are probably getting a header from somewhere else. Try:

 #include "C:\\fullPathToHeader\\Line.h" #include <iostream> using namespace std; void Line::display() const { cout << no << ' ' << item << endl; } 

also:

  • do not override MAX_CHARS in the cpp file.
  • Use security features for the title.
+5
source

To make Line :: display const, you don't need instance variable data.

 // Line.cpp #include "Line.h" #include <iostream> using namespace std; void Line::display() { cout << no << ' ' << Line::item << endl; } 
-5
source

Source: https://habr.com/ru/post/1416176/


All Articles