Reputation: 1
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
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,
Upvotes: 4