GhostWaggon
GhostWaggon

Reputation: 1

C++ header files in MS Visual Studio Error Message E0147

As a beginner programmer I'm learning the basics of C++ programming, including creation of header files. I'm following the professor's lecture on Youtube and recreated the same code structure to store class integers below. I am receiving the following compile error message for the class line.

Error (active) E0147 declaration is incompatible with "int MyInteger::get() const"

#include<iostream>

class MyInteger
{
    private:
        int i;
    public:

void set(int);

int get() const;
};

void MyInteger::set(int a)
{
    i = a;
}

void MyInteger::get() const
{
return i;
};


int main() {

    std::cout << "Hello, world!\n";

    std::cin.get();
}

Upvotes: 0

Views: 1617

Answers (1)

D-RAJ
D-RAJ

Reputation: 3380

Your error is from the editor and its stating that int MyInteger::get() const declaration is incompatible with the declaration in the class. Which is true because in the class your get() method is declared as,

int get() const;

But in your definition, its return type is set to void,

void MyInteger::get() const
{
    return i;
};

This is the error. To fix it, define the return type as int,

int MyInteger::get() const
{
    return i;
} // You dont need a semicolon here.

Additional:
There are 3 types of error messages in MSVC,

  1. Errors starting with 'E' which states that its from the editor.
  2. Errors starting with 'C' which states that its from the compiler (compile time).
  3. Errors starting with 'L' which states that its from the linker.

Upvotes: 4

Related Questions