我正在尝试我的第一个 C++ 项目,我想是从小规模开始的。我正在使用 Eclipse Luna 并尝试在所有可能的地方采用默认的构建环境。我怀疑以下是各种天真的代码,但你必须从某个地方开始。
我最终将在这方面有 4 个合作/子项目:
- 静态库的命令行界面
- .so"lib 将被动态加载到不同的第三方应用程序中并使用静态 lib
- 上面提到的静态库,用于执行组合应用的后端工作。
- 用于其他子项目通用的实用类和函数的实用类(目前,可能是一个小库)。
我想问的关于最佳实践、命名空间等的问题很多,但我会保持简短。
我有以下 C++ 头文件:
/*
* Utilities.h
*
*/
#ifndef UTILITIES_H_
#define UTILITIES_H_
// A
namespace UserTrackingUtilities {
// B
#include <string>
#include <exception>
using namespace std;
class MyException: public std::exception {
public:
MyException(std::string ss) : s(ss) {
}
~MyException() throw () {
} // Updated
std::string s;
const char* what() const throw () {
return s.c_str();
}
};
}
#endif /* UTILITIES_H_ */
这是一个异常实用程序(在不同的 StackOverflow 线程中找到),我将其包装在我自己的命名空间中——我认为。
Eclipse 显示了这个头文件的几个问题。我将从一个开始:它不喜欢 std::string
构造。我是否将 #include
和/或 using
语句放在 A
或 B
点并不重要>.
我也尝试过使用 Linux GCC 和 ADT 工具链。
欢迎指点和建议。
最佳答案
#include <string>
#include <exception>
应该早于
namespace UserTrackingUtilities {
顺便说一句:
如果你使用using namespace std
,你可以写string
而不是std::string
。
但我建议不要使用 using namespace std
以避免名称冲突和歧义。
更新:
这是一个最小的工作示例:
#include <iostream>
#include <string>
#include <exception>
namespace UserTrackingUtilities {
class MyException: public std::exception {
public:
MyException(std::string ss) : s(ss) {
}
~MyException() throw () {
} // Updated
std::string s;
const char* what() const throw () {
return s.c_str();
}
};
}
int main()
{
UserTrackingUtilities::MyException ex("Hello World");
std::cout << ex.what() << std::endl;
return 0;
}
关于c++ - linux eclipse c++ 本地命名空间和 "std::",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31997950/