C++ 包含内部类的外部类-将内部类实例化为外部类的成员

C++ 包含内部类的外部类-将内部类实例化为外部类的成员,c++,inner-classes,C++,Inner Classes,目前,我正在尝试编译以下代码: terminallo.hh #ifndef TERMINALLOG_HH #define TERMINALLOG_HH #include <string> #include <sstream> #include <iostream> class Terminallog { public: Terminallog(); virtual ~Terminallog(); class Warn {

目前,我正在尝试编译以下代码:

terminallo.hh

#ifndef TERMINALLOG_HH
#define TERMINALLOG_HH

#include <string>
#include <sstream>
#include <iostream>

class Terminallog {
public:

    Terminallog();
    virtual ~Terminallog();    

    class Warn {
    public:
        Warn();

    private:
        bool lineended;  
    };
    friend class Terminallog::Warn;

protected:

private:
    Warn warn;     

};
嗯,正如你可能猜到的,它失败了;-)。我的编译器说:

g++ src/terminallog.cc inc/terminallog.hh -o test -Wall -Werror 
In file included from src/terminallog.cc:8:
src/../inc/terminallog.hh:56: error: declaration of ‘Terminallog::Warn Terminallog::warn’
src/../inc/terminallog.hh:24: error: conflicts with previous declaration ‘void Terminallog::warn(std::string)’
这让我别无选择。我显然做错了什么,但是我不知道如何解决这个问题。如有任何提示,我将不胜感激

提前谢谢

ftiaronsem

警告此->警告()不是有效的C++语法——如果您想初始化<代码>警告>代码>成员,使用初始化列表(在这种情况下,不需要默认构造函数被隐式调用)。

阅读错误消息-您没有发布正确的代码-您有一个
Warn-Warn
成员变量和一个
void-Warn(std::string)
成员函数,重命名其中一个them@Erik. Uups,完全正确,令人印象深刻;-)。这个类中有更多的代码,我将其剥离。非常感谢你指出这一点。非常感谢你的回答。这很有趣。如何从另一C++文件中访问内部类的方法?我计划这样做:terminallo tlog();tlog.Warn.PubMethod();来自其他cpp文件。感谢advancelol,你已经在回答中写下了这一点。我刚刚读了一篇关于初始化列表的教程,然后我注意到了。非常感谢你。你真的应该很快达到10公里积分;-)
g++ src/terminallog.cc inc/terminallog.hh -o test -Wall -Werror 
In file included from src/terminallog.cc:8:
src/../inc/terminallog.hh:56: error: declaration of ‘Terminallog::Warn Terminallog::warn’
src/../inc/terminallog.hh:24: error: conflicts with previous declaration ‘void Terminallog::warn(std::string)’
Terminallog::Terminallog() : warn()
{
   // other constructor code
}


// note that `Warn::Warn()` is invoked implicitly on `wake`, so 

TerminalLog::Terminallog() {}

// would be equivalent