我刚刚将一个较大的项目从旧版本的Visual C ++移植到了VS2008,并注意到类视图使我的一堆类弄错了。在解决方案视图中,存在声明这些类的头文件,因此我希望可以在类视图中看到它们。是否出于某些原因将某些类排除在外的原因,或者是否有任何方法可以刷新类视图以在解决方案中包括所有类?
如果我想要一个循环引用,但在C++中的两个不同的文件中,我将如何实现它?
例如
AUnit.h
#inclue <BUnit.h>
class AClass : public TObject
{
__published
BClass * B;
};
Run Code Online (Sandbox Code Playgroud)
BUnit.h
#include <AUnit.h>
class BClass : public TObject
{
__published
AClass *A;
};
Run Code Online (Sandbox Code Playgroud)
我只能在一个带有前向声明的文件中创建它.
我有一个.h文件,其中包含几个类定义.我想在这个文件中使用C++的include guard; 但是,我想知道使用包含警卫的哪种方式被认为是正确/正确的?
一名警卫保护一切
#ifndef FOO_BAR
#define FOO_BAR
class Foo
{
};
class Bar
{
};
#endif
Run Code Online (Sandbox Code Playgroud)
或多个单独的警卫.
#ifndef FOO
#define FOO
class Foo
{
};
#endif
#ifndef BAR
#define BAR
class Bar
{
};
#endif
Run Code Online (Sandbox Code Playgroud) 我有一个名为"动物"的类,它是一个超级纯粹的多态.我有其他课程"狗","猫","仓鼠"等...都继承自"动物"类.
在一个程序中,我需要包括所有的子类(狗,猫,仓鼠),但是当我这样做时:
#include "Hamster.h"
#include "Dog.h"
...
...
Run Code Online (Sandbox Code Playgroud)
我收到错误:
先前对'class Animal'的定义
有办法防止这种情况吗?
PS我想过做一个:
#ifdef
Run Code Online (Sandbox Code Playgroud)
但是,如果该类被使用,那么包括它.例如,如果程序试图煽动Cat类,则执行包含.
对不起,如果没有解释清楚.
Eugh,一天有2个问题.我有一个你听到这么多的糟糕日子.我一直在组织我的小项目,以减少它的混乱.这是在开发的开始,所以没有太多的进展.我在下面有这个标题
#pragma once
#include <string>
class Game_Map
{
private:
int map_width;
int map_height;
string map_data [50][50]
public:
Game_Map(int default_width = 20,int default_height = 20)
~Game_Map()
};
Run Code Online (Sandbox Code Playgroud)
现在据我所见,应该没有任何问题.我避免使用"使用",我保持编程直到现在基本以防止外部干扰.但我是100%的时间得到"map.h:9:9:错误:'字符串'没有命名类型"
我确信我错过了什么.任何人都可以看到我哪里出错了?
我有一个用C编写的.inc文件.它包含一些带有实现的定义和方法签名.
.inc文件:
#define CONCAT(x,y) x ## y
#define LCONCAT(x,y) CONCAT(x,y)
#define DLIST LCONCAT(LCONCAT(double_,LISTELEMENT),_list)
#define DELETEDFIRST LCONCAT(delete_first_double_,LISTELEMENT)
int DELETEDFIRST (DLIST **first, DLIST **last);
int DELETEDFIRST (DLIST **first, DLIST **last)
{...}
Run Code Online (Sandbox Code Playgroud)
我想把它带到c ++.在我的.h文件中,我有define指令和方法签名(封装在命名空间中).在.cpp我只有方法实现.
.h文件
#define CONCAT(x,y) x ## y
#define LCONCAT(x,y) CONCAT(x,y)
#define DLIST LCONCAT(LCONCAT(double_,LISTELEMENT),_list)
#define DELETEDFIRST LCONCAT(delete_first_double_,LISTELEMENT)
namespace ListFunctions {
int DELETEDFIRST (DLIST **first, DLIST **last);
}
Run Code Online (Sandbox Code Playgroud)
.cpp文件
# include ListFunctions.h
namespace ListFunctions {
int DELETEDFIRST (DLIST **first, DLIST **last) {...}
}
Run Code Online (Sandbox Code Playgroud)
我打算在开发我的模块时使用这些列表函数.在我的module.h中,我定义了一个类型double_ node _list(双端),在我的module.cpp中,我将LISTELEMENT定义为" node ",然后包含ListFunctions.h.但ListFunctions.cpp中的相同内容会导致编译错误:
..\ListFunctions.h(86): error …Run Code Online (Sandbox Code Playgroud) 我目前正在编写一个程序,根据不同的参数搜索歌曲.在我的系统中有两种类型的歌曲:歌词和乐器.因为我需要将它们都放在1个向量中,所以我有一个歌曲类和一个LyricsSong&InstrumentalSong子类.
所以我有一个Song.h文件:
#include <stdio.h>
#include <iostream>
#include <string>
class Song
{
public:
std::string title;
virtual void print();
virtual void printSong(std::string query);
};
Run Code Online (Sandbox Code Playgroud)
还有乐器和歌词子类,它们以这种方式定义:
class LyricsSong : public Song
class InstrumentalSong : public Song
Run Code Online (Sandbox Code Playgroud)
两者都包括Song.h,在这两个类中,类只在头文件中定义.
当我尝试运行另一个使用这两个子类的文件时,包括:
#include "LyricsSong.h"
#include "InstrumentalSong.h"
Run Code Online (Sandbox Code Playgroud)
(显然更多的cpp库),我得到以下编译错误:
In file included from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/InstrumentalSong.h:16:0,
from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/songsParser.cpp:26:
/cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/Song.h:6:7: error: redefinition of 'class Song'
class Song
^
In file included from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/LyricsSong.h:15:0,
from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/songsParser.cpp:25:
/cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/Song.h:6:7: error: previous definition of 'class Song'
class Song
^
Run Code Online (Sandbox Code Playgroud)
什么时候: …