bgmrk
bgmrk

Reputation: 63

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.

Upvotes: 5

Views: 6723

Answers (2)

user1417475
user1417475

Reputation: 236

To make Line::display const implies you do not need instance variable data.

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

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

Upvotes: -5

Luchian Grigore
Luchian Grigore

Reputation: 258678

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.

Upvotes: 6

Related Questions