【问题标题】:C++ array parameters inside function not being declared未声明函数内的C++数组参数
【发布时间】:2017-03-25 15:13:11
【问题描述】:

我是 C++ 新手,如果这是一个愚蠢的问题,我深表歉意。我正在尝试创建一个在其参数中包含 3 个数组的函数。我收到了一个错误,即它们中的每一个都没有被声明。

标题中的代码: #ifndef 地址模型 #define ADDRESSMODEL #define ADDRESSDEBUG

#include <iostream>
#include <string.h>


using namespace std;
class PostCode
{
public:
     PostCode(void);
     ~PostCode();
     void postCodeCompare(tempPostCode[], theRoutingArray[], theIdentifier[]);

private:

    char theRoutingArray[4];
    char theIdentifier[5];
    char tempPostCode[8];
};

inline PostCode :: PostCode(void)
{
    strcpy( theRoutingArray, "000");
    strcpy( theIdentifier, "0000");
    cout << "Debug constructor called" << endl;
}

inline PostCode :: ~PostCode()
{
    cout<< "Destructor" << endl;
}

inline int PostCode :: postCodeCompare(tempPostCode, theRoutingArray, theIdentifier)
{
    char postCode[] = theRoutingArray + theIdentifier;
    if (postCode[0] == tempPostCode[0]){
        cout << 1 << endl;
    }
    else{
        cout << 0 << endl;
    }
}



#endif

主代码: #include "头文件.h" 使用命名空间标准;

int main( void){
cout << "main has started" << endl;

PostCode myCode;
char theRoutingArray[4];
char theIdentifier[5];
char tempPostCode[8];

cout << "Please type in your routing key: " << endl;
cin.getline(theRoutingArray, 4);

cout << "Please type in your identifier: " << endl;
cin.getline(theIdentifier, 5);

PostCode.postCodeCompare();

cout << "main has finished" << endl;


return 0;

}

非常感谢任何建议。

【问题讨论】:

  • 您没有正确声明 postCodeCompare 方法的输入,看起来它们只是您的私人成员。您还确定要在该方法中添加指针吗?我建议你切换到std::string 以获得更清晰的功能和更简单的代码。

标签: c++ arrays function parameters display


【解决方案1】:

建议:阅读一本很好的 C++ 书籍来解释基础知识。 C++ Primer 5th edition 就是一个例子。

您的数组参数语法不正确:您在声明中缺少数组元素的 type,并且缺少元素 type 和定义中的“数组语法”

此外,定义和声明之间的返回类型不匹配。

void postCodeCompare(char tempPostCode[], char theRoutingArray[], char theIdentifier[]);

...

inline int PostCode :: postCodeCompare(
    char tempPostCode[], char theRoutingArray[], char theIdentifier[]){ /*...*/ }

【讨论】:

  • 不幸的是,我仍然收到同样的错误“尚未声明 tempPostCode[]”。
  • 你也有一个返回类型不匹配。 postCodeCompare 声明为 void 并定义为 int。这是a working example。请去读一本 C++ 的书!
猜你喜欢
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-12
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多