【问题标题】:Why am I getting an error telling me the constructor is not naming a type?为什么我收到一个错误,告诉我构造函数没有命名类型?
【发布时间】:2013-03-03 21:20:35
【问题描述】:

我正在使用Code::Blocks。这是我的代码

#include "LargeInt.h"

LargeInt::LargeInt()
{

}

标题

#ifndef LARGEINT_H
#define LARGEINT_H


class LargeInt
{
    public:
        LargeInt();
};

#endif // LARGEINT_H

我得到的错误是

“LargeInt 未命名类型”在我的课程的第 3 行

我所做的只是单击文件 > 新建 > 类,然后在不更改任何设置或类似内容的情况下开始编码。

【问题讨论】:

  • 你想在构造函数中做什么?
  • 我在控制台应用程序和空项目中都试过了
  • 为了响应 stfrabbit,我将尝试使用我的重载插入运算符从用户那里获取输入
  • 你想在你的构造函数中用LargeInt::operator+(LargeInt){}做什么?
  • 您是否复制并粘贴了确切的代码?你确定你没有写#ifdef而不是#ifndef吗?您是否保存了LargeInt.h 文件?是不是和LargeInt.cpp保存在同一个目录下?

标签: c++ compiler-errors codeblocks


【解决方案1】:

您不应在构造函数中定义运算符。它们应该是 CPP 文件中的单独方法。

【讨论】:

  • 好点,我忘了。但这并不能改变错误。实际上,我只是在上课后对其进行了测试,然后就遇到了错误。这次我什至没有添加额外的代码。
  • 也许用干净的代码更新您的帖子,以便更容易审查。
  • 完成。不知道我能做到。
【解决方案2】:

构造函数应该执行使LargeInt 类型的对象进入有效状态所需的任何操作。似乎您正在尝试在构造函数中定义函数 operator<<operator+ - 您不能这样做:

LargeInt::LargeInt()
{
    LargeInt::operator<<(String input){}
    LargeInt::operator+(LargeInt){}
}

您应该定义具有类定义中相应声明的每个函数。您的实现文件应如下所示:

LargeInt::LargeInt()
{
    // ...
}

LargeInt LargeInt::operator<<(String str)
{
    // ...
    return some_large_int;
}

istream& operator>>(istream &is, LargeInt &large)
{
    // ...
    return is;
}

ostream& operator<<(ostream &os, LargeInt &large)
{
    // ...
    return os;
}

【讨论】:

  • 我完全搞砸了代码并没有考虑就发布了它-.- 但是,即使我删除了除创建类时存在的默认代码之外的所有内容,我仍然会遇到这个问题
猜你喜欢
  • 1970-01-01
  • 2021-02-13
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
相关资源
最近更新 更多