wie man Header-Dateien in c ++ verknüpft
Ich bin neu in der Programmierung in C ++ mit Header-Dateien. Das ist mein aktueller Code:
//a.h
#ifndef a_H
#define a_H
namespace hello
{
class A
{
int a;
public:
void setA(int x);
int getA();
};
}
#endif
//a.cpp
#include "a.h"
namespace hello
{
A::setA(int x)
{
a=x;
}
int A::getA()
{
return a;
}
}
//ex2.cpp
#include "a.h"
#include<iostream>
using namespace std;
namespace hello
{
A* a1;
}
using namespace hello;
int main()
{
a1=new A();
a1->setA(10);
cout<<a1->getA();
return 1;
}
Wenn ich versuche, es mit @ zu kompilierg++ ex2.cpp
, Ich erhalte diesen Fehler:
In function `main':
ex2.cpp:(.text+0x33): undefined reference to `hello::A::setA(int)'
ex2.cpp:(.text+0x40): undefined reference to `hello::A::getA()'
collect2: ld returned 1 exit status
Warum funktioniert es nicht und wie kann ich es beheben?