我用cocos2dx.当我使用它的类时,我需要cocos2d::经常键入,除非我输入using namespace cocos2d;.
如何避免不断重复命名空间?
我是WPF世界的开发人员,但我决定用C++完成所有Winrt开发.
让我感到困惑的一件事是using在头文件中使用(或不使用)语句(而不是键入完全限定的类型名称).
using在头文件中使用语句要容易得多.很多Microsoft Winrt示例都包含using标题中的语句,这些语句使代码清晰易读.但是,我一直在阅读John Petzolds新书预览中的c ++示例,并要求标题不受所有using陈述的影响.
我不了解专业人士和骗子,当我谷歌时,我得到了很多反对意见.
任何人都可以给我一个关于这个问题的明确解释,因为它与Winrt世界有关(我对其他环境中的c ++不感兴趣)
谢谢
我知道之前已经提出过一个非常相似的问题,"在c ++头文件中使用命名空间",但我的内部是命名空间.
namespace foo {
using namespace std; // what does this do?
class Bar { ... }
}
Run Code Online (Sandbox Code Playgroud)
这是否与其他问题完全相同,只是在所有地方使用声明?它只在该命名空间中执行吗?它只在标题中执行吗?
#ifndef MENU_H
#define MENU_H
#include <cstring>
#include <string>
#include <cctype>
class Menu{
public:
Menu();
void run();
int selectOptions();
void rotate90();
void rotate180();
void rotate270();
void flipVert();
void flipHoriz();
void convertHigh(int threshold);
void saveImage(string saveName);
void loadImage(string loadName);
void displayMenu();
private:
}
#endif // MENU_H
Run Code Online (Sandbox Code Playgroud)
C:\Users\Wattenphul\Documents\alg-prog design\project04>g++ -std=c++11 -o main.exe main.cpp menu.cpp
In file included from menu.cpp:6:
menu.h:22:20: error: 'string' has not been declared
void saveImage(string saveName);
^~~~~~
menu.h:22:35: error: expected ',' or '...' before 'saveName'
void saveImage(string saveName);
^~~~~~~~
menu.h:23:20: …Run Code Online (Sandbox Code Playgroud) 许多程序员告诉我,using namespace <x>在头文件中有一个语句是个坏主意.我一直盲目地遵循这个建议,直到现在却不明白为什么会如此.现在我正在开发一个包含大量命名空间的非常复杂的项目.有时我发现在开头告诉编译器关于命名空间而不是每次都要输入nested ::'是太诱人了.例:
ALongNameSpaceName::LongerNamespaceName::BasicUtilityFunctionUsedVeryCommonly
Run Code Online (Sandbox Code Playgroud)
这条规则背后的理由是什么?在哪些情况下我可以忽略此规则?
我正在使用 Visual Studio 2015 开发 C++ 解决方案。
我有一个带有此声明的 cpp 源文件和头文件 hpp。
标题:
#ifndef MyLib__FREEFUNCTIONS__INCLUDE__
#define MyLib__FREEFUNCTIONS__INCLUDE__
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
// Check if 'str' is null, empty or consists only of white-space characters.
inline bool IsNullOrWhiteSpace(string str);
// More functions
[ ... ]
#endif
Run Code Online (Sandbox Code Playgroud)
和源代码:
#include "FreeFunctions.h"
inline bool IsNullOrWhiteSpace(string str)
{
return (str.empty() || (str.find_first_not_of(' ') == string::npos));
}
Run Code Online (Sandbox Code Playgroud)
我在一个类中使用这个函数:
#include "ConvertToOwnFormat.h"
#include "FreeFunctions.h"
ConvertToOwnFormat::ConvertToOwnFormat()
{
}
ConvertToOwnFormat::~ConvertToOwnFormat()
{
}
vector<Entry> ConvertToOwnFormat::ReadCatalogue(string …Run Code Online (Sandbox Code Playgroud) 编译后面的源代码时出现问题:
QString code = this->viewManager()->activeView()->document()->text();
Qualifier qua;
qua.setQCode(code);
Run Code Online (Sandbox Code Playgroud)
它告诉我
error: undefined reference to `Qualifier::setQCode(QString)'
Run Code Online (Sandbox Code Playgroud)
qualifier.h和.cpp的代码如下
#ifndef __QUALIFIER_H__
#define __QUALIFIER_H__
#include <iostream>
#include <string>
#include <QString>
#include <queue>
#include "analyser.h"
using namespace std;
class Qualifier
{
private:
string code;
queue<method> tokenisedCode;
//queue<analysis> analysedCode;
void tokeniseCode();
public :
void setCode(string code);
void setQCode(QString code);
void computeLocAnalysis();
void computeCCAnalysis();
void visualiseResults();
};
#endif /* __QUALIFIER_H__ */
Run Code Online (Sandbox Code Playgroud)
而且CPP是
#include "qualifier.h"
using namespace std;
void Qualifier::tokeniseCode()
{
Parser par;
par.setCode(code);
par.parse();
this->tokenisedCode = par.getMethods();
} …Run Code Online (Sandbox Code Playgroud) c++ ×7
namespaces ×2
c++-cx ×1
coding-style ×1
g++ ×1
reference ×1
string ×1
using ×1
windows-8 ×1