Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not declared in scope, even though declared in .h

I have been stuck on this, my teacher doesn't even know what's going on. If someone could please help me that would be greatly appreciated.

I have declared item in the header file in the Line struct. However when calling on it in the Line::display() method, i get an error stating that the variable was not declared in the scope. I have showed my teacher and my peers and no one seems to know of 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 awesome. Thanks in advance.

like image 918
bgmrk Avatar asked Nov 13 '22 04:11

bgmrk


1 Answers

If this is your actual code, you're probably getting the header from somewhere else. Try:

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

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

Also:

  • don't re-define MAX_CHARS in the cpp file.
  • use include guards for the header.
like image 113
Luchian Grigore Avatar answered Dec 28 '22 00:12

Luchian Grigore