Это вопрос о нобе, извините, я прихожу с Java и понятия не имею, почему мой OO-материал не работает. У меня это главное:Неопределенная ссылка на Classname :: Classname() и другие ошибки
#include <iostream>
#include "Foo.h" //changed name
using namespace std;
int main(int argc, char*argv[])
{
int choice;
cin >> choice;
Foo net;
switch(choice)
{
case 1: net.buildNetwork(); break;
}
}
Этот Foo.h
файл:
#ifndef FOO_H
#define FOO_H
#include <iostream>
struct City{
std::string cityName;
std::string message;
City *next;
City(){}; // default constructor
City(std::string initName, City *initNext, std::string initMessage)
{
cityName = initName;
next = initNext;
message = initMessage;
}
};
class Foo
{
public:
Foo();
~Foo();
void addCity(std::string, std::string);
void buildNetwork();
void transmitMsg(char *); //this is like a string
void printNetwork();
protected:
private:
City *head;
City *tail;
};
#endif // FOO_H
И этот Foo.cpp
файл, все в том же каталоге:
#include "Foo.h"
#include <iostream>
using namespace std;
Foo::Foo()
{
head = tail = NULL;
}
Foo::~Foo(){}
void Foo::buildNetwork()
{
cout << "works" << endl;
}
void Foo::transmitMsg(){}
void Foo::printNetwork(){}
void Foo::addCity(){}
Когда я компилирую, я получаю
/tmp/ccNx3fY5.o: In function `main':
main.cpp:(.text+0x38): undefined reference to `Foo::Foo()'
main.cpp:(.text+0x4c): undefined reference to `Foo::buildNetwork()'
main.cpp:(.text+0x59): undefined reference to `Foo::~Foo()'
main.cpp:(.text+0x7e): undefined reference to `Foo::~Foo()'
collect2: error: ld returned 1 exit status
Что случилось? Также, еще один вопрос: в Foo.cpp
, зачем мне Foo::Foo()
и т. Д.? Я использовал namespace std
, так почему я не могу сказать Foo()
?
Что вы компилируете? –
'g ++ main.cpp -o main' –
Вы забыли включить foo.cpp при компиляции? Если вы просто скомпилируете main.cpp самостоятельно, то компилятор прав - ваша программа * не содержит эти функции, потому что foo.cpp не является частью вашей программы, если вы не говорите, что это так. – immibis