gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:31 25 4
gpt4 key购买 nike

<分区>

使用 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++ 的新手,但我已经在大学的计算机科学类(class)中,我经常使用 C# 并用 C 编写了一些东西...)。我认为它不能缺少任何引用或包含,因为它实际上处理 l1 声明......但是如果我在同一个源文件中创建一些其他类并以相同的方式实例化它,一切正常......

我用了this tutorial将库连接到他们使用的项目,所以这应该没问题。

我也为此进行了很多谷歌搜索,但事实证明,要么无法找到针对此问题的特定搜索词,要么我发现了某种特殊情况...

以下是上述类(class)的一些摘录:

  • 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() {

    }
    ...
  • 词法分析器.h:

    #ifndef LEXER_H_
    #define LEXER_H_

    #include "Token.h"

    namespace programma {

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com