类实现; 类接口{ public: Interface(int); ~Interface(); void setValue(int); int getValue()const; 私人: 实施* ptr; }; / * Interface.cpp * / #include" Implementation.h" #include" Interface.h" 接口::接口(int v):ptr(新的实现(v)) {} 接口:: ~Interface(){delete ptr; } void接口:: setValue(int v){ ptr-> setValue(v); } int接口:: getValue()const { 返回ptr-> getValue(); } / * t.cpp * / #include< iostream> using namespace std; #include" Interface.h" #include" Implementation.h" int main( ){ 接口i(5); cout<< i.getValue()<< endl; i.setValue(19); cout<< i.getValue()<< endl; 返回0; } #============这是Makefile #Makefile a.out:Implementation.h Interface.h Interface.cpp t.cpp g ++ Implementation.h Interface.cpp t .cpp 干净: rm -f a.out cleanall: rm -f * .h * .cpp a.outHi, here is an example I copied from Deitel C++ book. but when Icompile it, always get the above compilation error, no matter how Ichange the include order, please help.here is the files:Note: this is to practice Proxy classes.// Implementation.hclass Implementation {public:Implementation(int v) { this->v = v; }void setValue(int v){this->v = v;}int getValue() const{return v;}private:int v;};// Interface.hclass Implementation;class Interface {public:Interface(int);~Interface();void setValue(int);int getValue() const;private:Implementation *ptr;};/* Interface.cpp */#include "Implementation.h"#include "Interface.h"Interface::Interface(int v):ptr(new Implementation(v)){ }Interface::~Interface() { delete ptr; }void Interface::setValue(int v){ptr->setValue(v);}int Interface::getValue() const {return ptr->getValue();}/* t.cpp */#include <iostream>using namespace std;#include "Interface.h"#include "Implementation.h"int main(){Interface i(5);cout << i.getValue() << endl;i.setValue(19);cout << i.getValue() << endl;return 0;}# ============ this is Makefile# Makefilea.out: Implementation.h Interface.h Interface.cpp t.cppg++ Implementation.h Interface.cpp t.cppclean:rm -f a.outcleanall:rm -f *.h *.cpp a.out推荐答案 blueblueblue2005写道:blueblueblue2005 wrote:这是我复制的一个例子来自Deitel C ++的书。但是当我编译它时,总是得到上面的编译错误,无论我如何更改包含顺序,请帮忙。 嗯,您可能想要阅读错误消息:它清楚地说 编译器抱怨它应该要求 编译头文件。 #============这是Makefile #Makefile a.out:Implementation.h接口。 h Interface.cpp t.cpp g ++ Implementation.h Interface.cpp t.cpp Hi, here is an example I copied from Deitel C++ book. but when I compile it, always get the above compilation error, no matter how I change the include order, please help.Well, you might want to read the error message: it clearly saysthat the compiler complains about the request that it shouldcompile a header file. # ============ this is Makefile # Makefile a.out: Implementation.h Interface.h Interface.cpp t.cpp g++ Implementation.h Interface.cpp t.cpp 上面的行应该是 g ++ Interface.cpp t.cpp 。 - < mailto:di ** *********@yahoo.com> < http://www.dietmar-kuehl.de/> < http://www.eai-systems.com> - 高效的人工智能The above line should readg++ Interface.cpp t.cppinstead.--<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/><http://www.eai-systems.com> - Efficient Artificial Intelligence您好, 您可能需要更改您的makefile。与此同时,我不理解为什么你在头文件中定义函数的主体 文件就像你在Implementation.h中所做的那样。据我所知,。h 文件仅用于声明函数原型和变量。我们不用在头文件中写入函数体。这可能是导致这个编译错误的原因。 blueblueblue2005写道:Hi,May be you need to change your makefile. At the same time I dontunderstand why r u defining the body of the functions within headerfiles like u did in Implementation.h. As far as my knowledge goes ".h"files are for declaring function prototypes and variables only. We dontwrite the body of the function within a header file. That might be thereason why r u getting this compilation error.blueblueblue2005 wrote:这是我从Deitel C ++复制的一个例子书。但是当我编译它时,总是得到上面的编译错误,无论我如何更改包含顺序,请帮忙。 这里是文件:注意:这是练习代理类。 // Implementation.h 类实现{ public: Implementation(int v){this - > v = v; } void setValue(int v) { this-> v = v; } int getValue()const {返回v; } 私人: int v; }; // Interface.h 类接口{ public: Interface(int); ~Interface(); void setValue(int); int getValue()const; 私有:实现* ptr; }; / *接口.cpp * / #include" Implementation.h" #include" Interface.h" Interface :: Interface(int v):ptr (新的实现(v)) {} 接口:: ~Interface(){delete ptr; } void Interface :: setValue(int v){ ptr-> setValue(v); } int接口:: getValue ()const { return ptr-> getValue(); } / * t.cpp * / #include< iostream> ; 使用命名空间std; #include" Interface.h" #include" Implementation.h" int main(){接口i(5); cout<< i.getValue()<< endl; i.setValue(19); cout<< i.getValue()<< endl; 返回0; } #============这是Makefile #Makefile a。 out:Implementation.h Interface.h Interface.cpp t.cpp g ++ Implementation.h Interface.cpp t.cpp clean: rm -f a.out cleanall: rm -f * .h * .cpp a.out Hi, here is an example I copied from Deitel C++ book. but when I compile it, always get the above compilation error, no matter how I change the include order, please help. here is the files: Note: this is to practice Proxy classes. // Implementation.h class Implementation { public: Implementation(int v) { this->v = v; } void setValue(int v) { this->v = v; } int getValue() const { return v; } private: int v; }; // Interface.h class Implementation; class Interface { public: Interface(int); ~Interface(); void setValue(int); int getValue() const; private: Implementation *ptr; }; /* Interface.cpp */ #include "Implementation.h" #include "Interface.h" Interface::Interface(int v):ptr(new Implementation(v)) { } Interface::~Interface() { delete ptr; } void Interface::setValue(int v){ ptr->setValue(v); } int Interface::getValue() const { return ptr->getValue(); } /* t.cpp */ #include <iostream> using namespace std; #include "Interface.h" #include "Implementation.h" int main(){ Interface i(5); cout << i.getValue() << endl; i.setValue(19); cout << i.getValue() << endl; return 0; } # ============ this is Makefile # Makefile a.out: Implementation.h Interface.h Interface.cpp t.cpp g++ Implementation.h Interface.cpp t.cpp clean: rm -f a.out cleanall: rm -f *.h *.cpp a.out blueblueblue2005写道:blueblueblue2005 wrote:这是我从Deitel C ++书中复制的一个例子。但是当我编译它时,总是得到上面的编译错误 Hi, here is an example I copied from Deitel C++ book. but when I compile it, always get the above compilation error [snip] 什么错误?消息正文中没有包含错误。确保 确保将您的问题包含在邮件的*正文*中,而不仅仅是主题行 。 /丹[snip]What error? There in no error included in the body of the message. Makesure to include your problem in the *body* of the message not only inthe subject line./Dan 这篇关于g ++:请求的头文件的编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-27 19:01