Próbuję skompilować 2 klas w C++ za pomocą następującego polecenia:Zmienna C++ ma typ inicjatora, ale niekompletny?
g++ Cat.cpp Cat_main.cpp -o Cat
Ale pojawia się następujący błąd:
Cat_main.cpp:10:10: error: variable ‘Cat Joey’ has initializer but incomplete type
Może ktoś mi wyjaśnić, co to oznacza? Moje pliki zasadniczo tworzą klasę (Cat.cpp
) i tworzą instancję (Cat_main.cpp
). Oto mój kod źródłowy:
Cat.cpp:
#include <iostream>
#include <string>
class Cat;
using namespace std;
int main()
{
Cat Joey("Joey");
Joey.Meow();
return 0;
}
Cat_main.cpp:
#include <iostream>
#include <string>
using namespace std;
class Cat
{
public:
Cat(string str);
// Variables
string name;
// Functions
void Meow();
};
Cat::Cat(string str)
{
this->name = str;
}
void Cat::Meow()
{
cout << "Meow!" << endl;
return;
}
Nie można zadeklarować obiektów dla klas, których definicja nie jest jeszcze widoczna. – iammilind
Jak powinienem był widoczny? –
@Ken: Musisz dołączyć plik nagłówkowy. –