我是新来的代理类的概念,在C++中,当我尝试一个程序,我有错误,如未定义的引用功能。我不知道程序中哪里出了问题,请帮我纠正这些错误。未定义的引用功能接口::接口(int)
undefined reference to `Interface::Interface(int)'
undefined reference to `Interface::getValue() const'
undefined reference to `Interface::setVale(int)'
undefined reference to `Interface::~Interface()'
的代码如下
Implementation.h
#ifndef IMPLEMENTATION_H_INCLUDED
#define IMPLEMENTATION_H_INCLUDED
class Implementation
{
public:
Implementation(int v):value (v){}
void setValue(int v)
{
value=v;
}
int getValue()const{
return value;
}
private:
int value;
};
#endif // IMPLEMENTATION_H_INCLUDED
Interface.h
#ifndef INTERFACE_H_INCLUDED
#define INTERFACE_H_INCLUDED
class Implementation;
class Interface
{
private:
Implementation *ptr;
public:
Interface(int);
void setValue(int);
int getValue() const;
~Interface();
};
#endif // INTERFACE_H_INCLUDED
Interface.cpp
#include<iostream>
#include"Interface.h"
#include"Implementation.h"
using namespace std;
Interface::Interface(int v):ptr(new Implementation(int v))
{
}
void Interface::setValue(int v)
{
ptr->setValue(v);
}
int Interface::getValue()const
{
return ptr->getValue();
}
Interface::~Interface()
{
delete ptr;
}
和这里有云的主要功能 MainFunction.cpp
#include<iostream>
#include "Interface.h"
using namespace std;
int main()
{
Interface i(2);
cout<<"Interface Contains:"<<i.getValue()<<"before setValue"<<endl;
i.setValue(10);
cout<<"Interface contains:"<<i.getValue()<<"after setValue"<<endl;
return 0;
}
你似乎忘了将Interface.cpp添加到你的版本。 –
你可以显示你的编译命令行 – quantdev
在旁边注释...'{Interface x(1);界面y = x; } //哎呀,双删除。 –