c+中的代表+;(或类似的事情) 在C++中,我有: //Base1.h #ifndef BASE1_H #define BASE1_H #include <iostream> #include <string> #include "Base2.h" using namespace std; class Base1{ private: string name; public: Base1(string _name); void printSomething(); string getName(); }; #endif
和c+中的代表+;(或类似的事情) 在C++中,我有: //Base1.h #ifndef BASE1_H #define BASE1_H #include <iostream> #include <string> #include "Base2.h" using namespace std; class Base1{ private: string name; public: Base1(string _name); void printSomething(); string getName(); }; #endif,c++,delegates,C++,Delegates,和Base2()我像往常一样实现的构造函数,这是我的printBase1(base1b): void Base2::printBase1(base1b){ CUT自从 < 是C++中的指针,您需要取消引用: b2.printBase1(*this); 请注意,如果您有循环include,则应该从Base1.h中删除#include“Base2.h”。另外,还应查看通过(const)引用传递参数,特别是对于非POD类型,否则可能无法获得预期的行为 例如,您的签名是 void printBase1
Base2()
我像往常一样实现的构造函数,这是我的printBase1(base1b)
:
void Base2::printBase1(base1b){
CUT自从<代码> < <代码>是C++中的指针,您需要取消引用:
b2.printBase1(*this);
请注意,如果您有循环include,则应该从Base1.h
中删除#include“Base2.h”
。另外,还应查看通过(const
)引用传递参数,特别是对于非POD类型,否则可能无法获得预期的行为
例如,您的签名是
void printBase1(Base1 b);
调用时,在函数中创建参数的副本,从而对副本进行操作。应将其更改为:
void printBase1(Base1& b);
或
只有当你确定你需要一份副本时才传递值。如果我删除Base2.h
,我如何在printSomething()中创建Base2 b2;
?我得到一个错误:(!@Kingfisher你将它包括在cpp文件中,而不是标题中。谢谢,但我不会更改作废printBase1(Base1 b)
,我仍然得到了一个正确的结果!我幸运吗?最近我遇到了一个问题:无法将'this'指针从'const Base1'转换为'Base1&'
?我有什么问题吗?如果我删除每个const
,它会再次工作!!@Kingfisher您正在尝试修改一个const对象,这就是问题所在。
void printBase1(Base1 b);
void printBase1(Base1& b);
void printBase1(const Base1& b); //if you don't change b