【发布时间】:2014-06-22 13:51:34
【问题描述】:
由于某些奇怪的原因,我很难在 C++ 中抛出异常。我在没有从 stdexcept 头文件中捕获 std::invalid_argument 的情况下抛出。我没有真正的捕捉意图,因为如果发生错误,我希望应用程序无论如何都会失败。
在我将函数定义类#included 到标头声明的命名空间之前,它似乎工作正常。它是在命名空间之前添加的,因为它们是模板定义,我想将标题与其定义分开;然而,我意识到这引起了一个我直到最近才意识到的微妙问题。
他们是我缺少的东西吗?我正在使用clang btw
Project Compilation
.
.
.
.
.
Compiling CPP file TrieTest.cpp ...
In file included from TrieTest.cpp:4:
In file included from ./Trie.hpp:62:
In file included from ./Trie.cpp:2:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/stdexcept:55:30: error: unknown class name 'exception'; did you mean
'::std::exception'?
class logic_error : public exception
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/exception:60:9: note: '::std::exception' declared here
class exception
^
In file included from TrieTest.cpp:4:
In file included from ./Trie.hpp:62:
In file included from ./Trie.cpp:2:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/stdexcept:112:32: error: expected class name
class runtime_error : public exception
^
2 errors generated.
编辑: 一点 src 我也编译 clang++ -Wall -Wextra -g -std=c++11 TrieTest.cpp -o TrieTest;
“Trie.h”
#ifndef COM_WORDGAME_UTILITY_TRIE_HPP
#define COM_WORDGAME_UTILITY_TRIE_HPP
#include <string>
using std::string;
namespace wordgame_utility{
template<typename value>
class Trie{
...Trie Function Declarations
};
//The compiler may not complain initialliy however
//Templates cause visibility issues with user code and is normally defined in the header
//this is a work around
#include "Trie.cpp"
}
#endif
"Trie.cpp" 头 -n 8
#include "Trie.hpp"
#include <stdexcept>
using namespace wordgame_utility;
template<typename value>
using TrieNode = typename Trie<value>::TrieNode;
...Trie Function Definitions
【问题讨论】:
-
'我缺少他们的东西吗?我正在使用clang btw'很难看到没有看到实际发生错误的代码。您是否只是错过了
#include <exception>或错过了指定命名空间std::exception?或者作为另一个可能的错误来源:您是否使用-std=C++11标志进行编译? -
@πάνταῥεῖ:
stdexcept是标准标头,所以这有点奇怪。标头中的代码应该是自给自足的,但它可能已经被混淆了,或者它们可能是 libstdc++ 标头和 clang 的选项之间的不匹配。 -
你没有把你的
#include <exception>放在一个命名空间里吗? -
你能上传一个导致这个错误的代码示例吗(我想它应该是
#include <stdexcept>的顺序,然后上传到gist.github.com或类似clang -E test.cc的输出?另外,您安装了哪个版本的 libstdc++? -
感谢您的快速回复............我添加了部分来源我原本不想让问题太长............ ...我用 clang++ -Wall -Wextra -g -std=c++11 TrieTest.cpp -o TrieTest 编译;
标签: c++ c++11 exception-handling clang