C++:无法用new实例化我的类
C++: Cannot instantiate my class with new
使用Eclipse CDT,我编写了一个抽象类"Lexer",它驻留在一个共享库项目中。它由"UTF8Lexer"在另一个共享库项目中继承。为此,我创建了一个UnitTest++测试项目,其中包含以下代码:
#include "UnitTest++.h"
#include "UTF8Lexer.h"
#include <fstream>
using namespace std;
programma::Lexer<UChar32, icu::UnicodeString>* getLexer(string sampleFile)
{
string path = "../samples/" + sampleFile;
ifstream* stream = new ifstream();
stream->open (path.data());
programma::UTF8Lexer l1(stream); //This line compiles fine.
programma::UTF8Lexer* l2 = new programma::UTF8Lexer(stream); // Error: "Type 'programma::UTF8Lexer' could not be resolved"
return l2;
}
我不明白为什么他喜欢l1的声明,但不喜欢l2的声明…这个典型的不具体的错误消息并没有给我太多线索(虽然我是C++的新手,但我在大学的计算机科学课程中用C#做了很多工作…(。我认为它不能是任何遗漏的参考或包含,因为它实际上处理l1声明。。。但是,如果我在同一个源文件中创建其他类,并以相同的方式实例化它,一切都会正常。。。
我使用本教程将库连接到它们的使用项目,所以这应该很好。
我也在谷歌上搜索了很多,但事实证明,要么无法找到这个问题的特定搜索词,要么我发现了某种特殊情况。。。
以下是上述课程的节选:
UTF8Lexer.h:
#ifndef UTF8LEXER_H_ #define UTF8LEXER_H_ #include "unicode/unistr.h" #include "Lexer.h" #include <iostream> using namespace icu; namespace programma { class UTF8Lexer : public Lexer<UChar32, UnicodeString> { public: UTF8Lexer(std::istream* source); ~UTF8Lexer(); ...
UTF8Lexer.cpp:
#include "UTF8Lexer.h" namespace programma { programma::UTF8Lexer::UTF8Lexer(std::istream* source) { } programma::UTF8Lexer::~UTF8Lexer() { } ...
Lexer.h:
#ifndef LEXER_H_ #define LEXER_H_ #include "Token.h" namespace programma { template<typename C, typename S> class Lexer { public: ...
programma::UTF8Lexer l1(stream);
可能被解析为programma::UTF8Lexer l1(std::stream __Unnamed_Argument);
,即一个名为l1
的函数的声明。删除using namespace std::
以解决此问题。
我发现了造成我麻烦的原因:正确地将"UTF8Lexer"命名为"UTFLxer"解决了所有问题。但是,几个小时后,我和一个班上的成员也遇到了同样的问题。在处理完这个看起来完全不起作用的Eclipse/CDT/GCC设置几分钟后,我想到了为项目重建索引的想法:只需右键单击项目,选择"索引"->"重建"。现在它起作用了。
相关文章:
- 为什么我们不在下面给出的代码中使用指针来实例化C++的实体对象?
- 为什么我不能引用指向实例化对象的函数的指针?
- OSX clang++:用于 cpp 文件中显式实例化模板的体系结构x86_64的未定义符号
- 您能否实例化模板的非专用版本并在专用化中继承它?
- 请参阅在 Visual Studio 2019 中捕获 std::exception 时对函数模板实例化消息的引用
- 类模板实例化中的类型转换
- clang:使用 O3 导出隐式实例化函数的符号
- 用字符串初始化我的类的对象
- 如何实例化类的公共成员并将其作为 std::p romise 返回?
- 在 Xcode 中实例化后的显式专用化
- 有没有办法根据 lambda 参数返回类型部分专用化我的模板化函数?
- 使用 SFINAE 有选择地实例化模板的成员函数
- 模板类实例化中的指针转换无效
- 无法实例化重载的模板化函数
- 尝试在构造函数中的"glm::cross"处初始化我的类段错误
- 与模板实例化相关的编译错误
- 如何使用 C++11 unique_ptr实例化我的代码
- 当丢失非const虚拟方法时,为什么我不能实例化类的const实例
- 我可以使用decltype()来避免显式模板实例化中的代码重复吗
- C++:无法用new实例化我的类