gpt4 book ai didi

C++ 自定义用户定义主要?

转载 作者:行者123 更新时间:2023-11-28 07:25:00 24 4
gpt4 key购买 nike

我对提供给我们的这段代码感到非常困惑:

class KeyedWord
{
public:
KeyedWord(char* word); // TO BE IMPLEMENTED
char* getWord(); // TO BE IMPLEMENTED
char* getKey(); // TO BE IMPLEMENTED
~KeyedWord();
private:
char * _word;
char* _key;
};
void main(int argumentCount, char** arguments)
{
if (argumentCount <= 1)
{
std::cout << "No file name given as argument" << std::endl;
}
else
{
}

我很困惑,因为:

如果 main 是程序的起源,我应该如何为 main 提供重载?

此外,当我尝试实现上述功能之一时,我收到一条错误消息,告诉我存在定义错误。这就是我正在尝试的:

void KeyedWord::getWord()
{

return;
}

最佳答案

您不能重载 main 函数。

来自标准:

3.6.1 Main function [basic.start.main]

2/ An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both:

  • a function of () returning int and
  • a function of (int, pointer to pointer to char) returning int

你也声明

 class KeyedWord
{
public:
// ...
char* getWord(); // TO BE IMPLEMENTED

// ...
};

然后您将方法定义为

   void KeyedWord::getWord() { ... }
// ^^^^

返回类型不正确...

关于C++ 自定义用户定义主要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18912852/

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