我刚开始用C++编程,我试图创建两个类,其中一个将包含另一个.
档案A.h:
#ifndef _A_h
#define _A_h
class A{
public:
A(int id);
private:
int _id;
B _b; // HERE I GET A COMPILATION ERROR: B does not name a type
};
#endif
Run Code Online (Sandbox Code Playgroud)
档案A.cpp:
#include "A.h"
#include "B.h"
#include <cstdio>
A::A(int id): _id(id), _b(){
printf("hello\n the id is: %d\n", _id);
}
Run Code Online (Sandbox Code Playgroud)
档案B.h:
#ifndef _B_h
#define _B_h
class B{
public:
B();
};
#endif
Run Code Online (Sandbox Code Playgroud)
档案B.cpp:
#include "B.h"
#include <cstdio>
B::B(){
printf("this is hello from B\n");
} …Run Code Online (Sandbox Code Playgroud) 我试图查找虚拟函数是在编译期间还是在运行时确定。在查看时,我发现了一些动态链接/后期绑定,但我不明白这是否意味着函数本身在可执行文件之前的编译期间或可执行文件期间确定。
有人可以解释一下吗?
可能重复:
C++ Virtual/Pure Virtual Explained
c ++中虚函数实例化的区别
为什么纯虚函数初始化为0?
这是有人给我的一些类声明中的方法.我不知道'.. = 0'是什么意思.它是什么?
virtual void Print() const = 0;
Run Code Online (Sandbox Code Playgroud) 我写了两个课,Agent并且Timing.第三类将包含该main()功能并对其进行管理.目标是创建n个实例Agent和一个SINGLE实例Timing.重要的是要提到Agent使用Timing字段和Timing使用Agent函数.我怎么能拒绝Timing给独居?
//Agent.h
#ifndef Timing_h
#define Timing_h
#include <string>
#include "Timing.h"
class Agent{
public:
Agent(std::string agentName);
void SetNextAgent(Agent* nextAgent);
Agent* GetNextAgent();
void SendMessage();
void RecieveMessage(double val);
// static Timing runTime;
Run Code Online (Sandbox Code Playgroud)
我认为可以解决我的问题,但我得到了:
'Timing'没有命名类型
~Agent();
private:
std::string _agentName;
double _pID;
double _mID;
Agent* _nextAgent;
};
#endif
//Timing.h
#ifndef Timing_h
#define Timing_h
class Timing{
private:
typedef struct Message{
Agent* _agent;
double _id; …Run Code Online (Sandbox Code Playgroud) 我想我的makefile有问题.我正在写这个程序:
Q2.cpp 包含主要.Agent.cpp Agent.hTiming.cpp Timing.hRandomDouble.cpp RandomDouble.cpp而我用的标题randoma.h在RandomDouble.cpp.我下载了randomaelf64.a文件并编写了这个makefile:
Q2 : Q2.o Agent.o Timing.o RandomDouble.o
g++ -Wall -g randomaelf64.a RandomDouble.o Q2.o Agent.o Timing.o -o Q2
Q2.o : Q2.cpp Agent.h Timing.h
g++ -Wall -g -c Q2.cpp -o Q2.o
Agent.o : Agent.cpp Agent.h Timing.h RandomDouble.h PrintQ2.h
g++ -Wall -g -c Agent.cpp -o Agent.o
RandomDouble.o : RandomDouble.cpp RandomDouble.h randoma.h
g++ -Wall -g -c RandomDouble.cpp -o RandomDouble.o
Timing.o : Timing.cpp Timing.h Agent.h
g++ -Wall -g -c Timing.cpp …Run Code Online (Sandbox Code Playgroud)