小编or.*_*ore的帖子

类名未在C++中命名类型

我刚开始用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++

29
推荐指数
3
解决办法
11万
查看次数

虚拟函数是在编译期间确定的?

我试图查找虚拟函数是在编译期间还是在运行时确定。在查看时,我发现了一些动态链接/后期绑定,但我不明白这是否意味着函数本身在可执行文件之前的编译期间或可执行文件期间确定。

有人可以解释一下吗?

c++ virtual

5
推荐指数
1
解决办法
3001
查看次数

3
推荐指数
1
解决办法
591
查看次数

在我的C++程序中使用Singleton

我写了两个课,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)

c++ singleton

2
推荐指数
1
解决办法
606
查看次数

我的makefile有问题

我想我的makefile有问题.我正在写这个程序:

  • Q2.cpp 包含主要.
  • Agent.cpp Agent.h
  • Timing.cpp Timing.h
  • RandomDouble.cpp RandomDouble.cpp

而我用的标题randoma.hRandomDouble.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)

c++ makefile

1
推荐指数
1
解决办法
174
查看次数

标签 统计

c++ ×5

makefile ×1

pure-virtual ×1

singleton ×1

virtual ×1