C++:无法用new实例化我的类

C++:无法用new实例化我的类,c++,types,C++,Types,使用EclipseCDT,我编写了一个抽象类“Lexer”,它驻留在一个共享库项目中。它由另一个共享库项目中的“UTF8Lexer”继承。为此,我创建了一个UnitTest++测试项目,其中包含以下代码: #include "UnitTest++.h" #include "UTF8Lexer.h" #include <fstream> using namespace std; programma::Lexer<UChar32, icu::UnicodeString>*

使用EclipseCDT,我编写了一个抽象类“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;
}
Lexer.h:

#ifndef LEXER_H_
#define LEXER_H_

#include "Token.h"

namespace programma {

template<typename C, typename S> class Lexer {
public:
...

programma::UTF8Lexer l1stream;可能被解析为programma::UTF8Lexer l1std::stream uu Unnamed_参数;,i、 e.名为l1的函数的声明。使用namespace std::删除以修复此问题。

我发现了导致问题的原因:正确地将“UTF8Lexer”赋釉到“UTFLexer”解决了所有问题。但是,几个小时后,我和一个班上的同学有同样的问题。在处理这个似乎完全失效的Eclipse/CDT/GCC设置的几分钟后,我想到了为项目重建索引:右键单击项目,选择index->Rebuild。现在它可以工作了。

您使用的是哪种编译器,它会向您发送错误消息?内食错误是命中和错过,不值得尝试调试,所以我假设它是一个正确的编译错误,你正在展示给我们。我使用GNU C++工具链,是的,它是一个正确的编译器错误。在UTF8LFROUP。CPP中,你不需要编程::因为您已经在programma名称空间中了。我应该提到,添加l1声明只是为了证明,实例化UTF8Lexer确实有效。仅使用“new”分配堆实例失败…stream真的是std::的成员吗?即使l1被解析为函数,为什么会影响下一行的解析?@CharlesBailey:可能,这取决于实现。它不会影响下一行的解析,但是函数声明会编译,即使programma::UTF8Lexer是不完整的类型。好吧,它不应该由实现决定。如果流不是std::的显式部分,那么由于它不以uu或包含uuu开头,因此允许用户将其用作宏,因此标准库实现不应将其用于任何冲突的目的。@CharlesBailey:确实允许您定义这样的宏,但这并不一定与std冲突,我不确定我是否理解你的意思。您是说允许实现在标准头文件的std名称空间中定义流吗?有人知道Eclipse索引与GNU编译器有什么关系吗?为什么一个会影响另一个?
#include "UTF8Lexer.h"

namespace programma {

programma::UTF8Lexer::UTF8Lexer(std::istream* source)
{
}

programma::UTF8Lexer::~UTF8Lexer() {

}
...
#ifndef LEXER_H_
#define LEXER_H_

#include "Token.h"

namespace programma {

template<typename C, typename S> class Lexer {
public:
...