【问题标题】:Can't call function from public class in main ... "new types may not be defined in a return type" [closed]无法从 main 中的公共类调用函数......“新类型可能未在返回类型中定义”[关闭]
【发布时间】:2013-11-23 21:31:48
【问题描述】:

我正在尝试在公共类中调用 void 函数,但出现我不理解的错误:

#include <iostream>
class Buttons
{
    public:
        Buttons()
        {
            short pushl;
            short *tail;
            cout << "Wally Weasel" << "/t";
            void init_sub(int x, int y);
        };
        ~Buttons()
        {
            cout << "Buttons has been destroyed!";
        };
}
int main(int args, char**LOC[])
{
    int z, a;
    Buttons::init_sub(z, a);
    return 2;
}
Buttons::void init_sub(int x, int y)
{
    cout << &x << &y;
}

新更新的代码(仍然不起作用):

#include <iostream>
using namespace std;

class Buttons
{
  public:
  Buttons()
  {
    short pushl;  // unused variable in Constructor: should be a member variable?
    short *tail;  // same
    cout << "Wally Weasel" << "/t";
  };

  ~Buttons()
  {
    cout << "Buttons has been destroyed!";
  }

 void init_sub(int z, int a);
};


int main(int args, char **LOC[])
{
    int z = 0;
    int a = 1;
    Buttons::init_sub(z, a);
    return 2;
}

void Buttons::init_sub(int x, int y)
{
    cout << &x << " " << &y;
}

为什么我不能调用函数?

原始错误持续存在:“新类型可能未在返回类型中定义”

PS:我更新了我的代码以匹配我目前的情况 - 但仍然是同样的错误。 我一直在努力使用 C++ - 我习惯于低级编程,没有涉及语法/结构的这么多语义。

【问题讨论】:

  • 有点乱,你想做什么?
  • 如果你缩进你的代码,每个人都会更清楚——包括你。
  • 缩进是一个有一个含义的词。他是这个意思。
  • 我只想说我们中的许多人一定在想什么。先去学习 C++ 的基础知识。了解哪些类与对象有何不同,如何用 C++ 正确编写它们。
  • 另外,忠告,小心不要进入“你必须努力回答我的问题”的领域。这里的人不工作,我们不需要放任何东西。由你来努力让你的问题得到回答。如果有人说他们不知道你的意思,那就解释一下。相信我,如果你付出努力,你会发现这个社区更愿意提供帮助

标签: c++ oop object


【解决方案1】:

函数 init_sub 声明在错误的位置。它必须从构造函数体移到类声明中。

您不能调用该函数,因为它是一个非静态成员函数。它需要一个实例来调用该函数。你没有提供一个。要么在实例上调用它,要么将其设为静态。

您的主要功能也有错误的签名。应该是

int main(int argc, char* argv[])

【讨论】:

  • 以前从未听说过“实例方法”这个词。阐明?我也试过static,还是不能调用;见上面的错误。
  • 与非静态成员函数相同,但实例方法少了一些拗口。实例方法不是 C++ 的常用术语。不过,该功能需要一个主题,而您没有提供主题。
  • 无论如何,我的代码仍然给我同样的原始错误。
  • 然后用我建议的方法之一修复它。
【解决方案2】:

我认为这就是你想要做的。请尝试缩进您的代码,尤其是在向他人寻求帮助时。

编译版本:http://ideone.com/9lGDvn

#include <iostream>
using namespace std;

class Buttons
{
  public:
  Buttons()
  {
    short pushl;  // unused variable in Constructor: should be a member variable?
    short *tail;  // same
    cout << "Wally Weasel" << "\t";  // Corrected tab character
  };

  ~Buttons()
  {
    cout << "Buttons has been destroyed!";
  }

  static void init_sub(int z, int a);
};

// Note that second argument to main should be char* loc[], one fewer pointer attribute
int main(int args, const char* const LOC[])
{
    int z = 0;
    int a = 1;
    Buttons::init_sub(z, a);
    return 2;
}

void Buttons::init_sub(int x, int y)  // not "Buttons::void"
{
    cout << &x << " " << &y;
}

【讨论】:

  • 我仍然得到与上述相同的原始错误。
  • 使用复制粘贴,以便您实际运行此代码。你需要更加努力!
  • 我复制粘贴了,还是一样的错误。 @DavidHeffernan 你想在这里侮辱我吗?
  • Always Errors,你能粘贴确切的错误,然后告诉我们这个程序的哪一行给你一个错误吗?我在 ideone.com 上包含了一个使用在线编译器编译和运行的程序的链接
  • 确实,您的main(int, char***) 的签名与我的程序不同,后者具有main(int, char**)(带有额外的常量)。
猜你喜欢
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 2014-09-10
相关资源
最近更新 更多