在切换到C++之前,我们发现initializationDelphi中的语言元素非常有用.它允许您在程序启动时调用每个单元中的代码,因此您可以初始化该单元的各种元素.
我们认为这可以使事情变得更容易并有助于保持代码清洁.
那么为什么没有initialization和finalizationC++中?
在C++中替换这种语言功能有哪些选择?
这听起来很奇怪,但也许存在一个...我用Google搜索,但没有找到任何东西.
简单的例子:
我有一个文件class1.h:
#include "a.h"
#include "b.h"
Run Code Online (Sandbox Code Playgroud)
另一个文件class2.h:
#include "a.h"
#include "c.h"
Run Code Online (Sandbox Code Playgroud)
和main.cpp:
#include "class2.h" //as we see here, we getting "a.h" double included by class1.h and class2.h
Run Code Online (Sandbox Code Playgroud)
我想在我的项目中摆脱这种欺骗.
当然,在例子中并没有那么难,但我有大量的文件,它们在很多方面相互包含,很难自己追踪所有的欺骗.
在我自己编写该工具之前,有什么建议吗?:)
声明:"必须在调用内联函数之前定义它们."
这个陈述是否正确?
[编辑]
问题最初是德语:
Inline-Funktionenmüssenvorihrem Aufruf definiert sein.
也许它可以帮助任何人......
我是新手使用头文件等,上学期我们在一个巨大的(可怕的:p)文件中做了所有事情......
我在做一些我不应该做的事吗?尝试运行该程序会导致以下结果:
1> LINK : ~~~\CSC 161\Accounting Assignment\Debug\Accounting Assignment.exe not found or not built by the last incremental link; performing full link
1>driver.obj : error LNK2005: "class std::basic_ifstream<char,struct std::char_traits<char> > welcomeFile" (?welcomeFile@@3V?$basic_ifstream@DU?$char_traits@D@std@@@std@@A) already defined in statistics.obj
1>~~~~\CSC 161\Accounting Assignment\Debug\Accounting Assignment.exe : fatal error LNK1169: one or more multiply defined symbols found
1>
Run Code Online (Sandbox Code Playgroud)
statistics.h:
#ifndef _STATISTICS_INTERFACE_
#define _STATISTICS_INTERFACE_
...
#include<fstream>
using namespace std;
ifstream welcomeFile; //if I comment this out, it compiles
class Stats
{
...blah...
};
void welcome();
void pause(); …Run Code Online (Sandbox Code Playgroud) 这之间的实际区别是什么
//Foo.h
struct Foo {
void bar() {
//lots of complex statements
}
};
Run Code Online (Sandbox Code Playgroud)
还有这个
//Foo.h
struct Foo {
void bar();
};
//Foo.cpp
inline void Foo::bar() {
//lots of complex statements
}
Run Code Online (Sandbox Code Playgroud)
这两种方法在最终编译程序中是否有任何差异,或者保证是否相同?
还请就哪些人应该选择以及为什么在良好的编码实践/经验方面做出一些评论.请注意"许多复杂的陈述".这些事情应该在头文件中的任何特定情况?AFAIK,大多数提升库只是标题 - 他们为什么选择这样做?
我正在使用早期版本的Cocos2dx来编写游戏并使用VS 2013进行编译.请注意,我正在使用CMake和Qt Creator两种编译器版本.当Cocos2dx v3.12问世时,我决定在我的游戏中将lib升级到该版本并开始使用VS 2015.然后我开始收到此错误:
QCardManager.cpp.obj:-1:错误:LNK2001:未解析的外部符号"public:static class QCard*__cdecl QCard :: create(enum PLAYER,struct Question const*,enum CARD_TYPE,int const&)"(?create @ QCard @@ SAPAV1 @ W4PLAYER @@ PBUQuestion @@ W4CARD_TYPE @@ ABH @ Z)
当我使用VS 2013时,我没有得到那个错误.经过几个小时的调试后我发现了原因.
这是粗略的贬值QCard:
#include "2d/CCSprite.h"
#include "CommonVariables.h"
class RandomPostureSprite;
class Question;
namespace cocos2d
{
class Label;
}
enum class CARD_TYPE {
QUESTION,
OPTION
};
class QCard : public cocos2d::Sprite
{
public:
static QCard *create(PLAYER player, const Question *question, CARD_TYPE type, const int &index);
}
Run Code Online (Sandbox Code Playgroud)
我在QCard.cpp文件中正确实现了该功能,并且该文件也正确地添加到了项目中.所以问题是class …
假设您的头文件中有一个静态全局变量,并在main.cpp中使用此变量.
// header.h
static int variableOne = 100;
//main.cpp
.
.
cout << variableOne << endl;
Run Code Online (Sandbox Code Playgroud)
main.cpp会得到自己的variableOne副本(虽然值仍然是100 ......)?或者我将这个概念与extern混合(我知道extern告诉编译器,variableOne是在项目的其他地方定义的......)
谢谢.
我偶然发现了一个我可以解决的问题,但我不确定它为什么不起作用.
这是我试图使用的代码.为了简洁起见,已经剥离了字段.让我知道他们是否需要,我会把它们放回去:
#pragma once
#ifndef __PageStyle__
#define __PageStyle__
class PageStyle
{
public:
friend bool operator<(const PageStyle& lhs, const PageStyle& rhs);
};
bool operator<(const PageStyle& lhs, const PageStyle& rhs)
{
return (lhs.name < rhs.name);
}
#endif
Run Code Online (Sandbox Code Playgroud)
在我的源文件中,我做了类似这样的事情:
#include "PageStyle.h"
...
void PageStyleManager::loadPageStyles() {
std::set<PageStyle> pageStyles;
...
}
Run Code Online (Sandbox Code Playgroud)
编译好的代码,但链接器吐了出来:
1>PageStyleManager.obj : error LNK2005: "bool __cdecl operator<(class PageStyle const &,class PageStyle const &)" (??M@YA_NABVPageStyle@@0@Z) already defined in BaseContentFiller.obj
Run Code Online (Sandbox Code Playgroud)
BaseContentFiller是PageStyleManager的基类,也是以类似方式使用PageStyle的其他类.
经过深入研究后,我发现为了我的目的(使用STL集中的类)我毕竟不需要非成员朋友版本.我让操作员成为一个内联的公共成员,并且代码链接没有问题.
为什么会出现这个问题?我确保我使用过标题保护,这是我第一次遇到运算符重载的真实体验,我想知道我做错了什么.
我写的
// In file t.h
#ifndef __t_h__
#define __t_h__
static int abc;
#endif
Run Code Online (Sandbox Code Playgroud)
-
//In main.c
#include <stdio.h>
#include "t.h"
int main()
{
abc++;printf("%d \n", abc);
test();
}
Run Code Online (Sandbox Code Playgroud)
- -
//In test.c
#include <stdio.h>
#include "t.h"
void test()
{
abc++;
printf("%d \n", abc);
}
Run Code Online (Sandbox Code Playgroud)
当我运行该项目,我发现output的abc is 1 and 1.但是当我改变它int abc时t.h.的输出abc = 1 and 2.为什么静态不会在控件到达test.c文件时保留该值.如果它不会保留那么为什么它不应该提供错误作为static variable can not be shared between/among文件?
我用C++/cocos2dx 遇到了"AppDelegate.obj中已定义的错误".
这是我的代码gamestage.h
#ifndef __GAME_STAGES_H__
#define __GAME_STAGES_H__
// stage 1;
namespace gamestage1
{
int btn_number = 9;
}
#endif
Run Code Online (Sandbox Code Playgroud)
game.cpp并menu.cpp使用此gamestage.h文件,没有gamestage.cpp文件.
实际上,我尝试使用extern如下:
extern int btn_number = 9;
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
*这会导致什么?*
c++ ×10
boost ×1
c ×1
delphi ×1
duplicates ×1
include ×1
linker ×1
namespaces ×1
visual-c++ ×1