2016-11-11 83 views
-1

我是C++新手,所以如果这是一个愚蠢的问题,我很抱歉。我试图创建一个函数,它的参数是3个数组。我收到错误,他们每个人都没有被宣布。函数内部的C++数组参数没有声明

代码在头: 的#ifndef ADDRESSMODEL 的#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 “Header.h” 使用命名空间std;

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; 

} 

任何意见是非常感谢。

+0

您没有正确声明输入您的postCodeCompare方法,它看起来就像他们是为了仅仅是你的私有成员。你也确定你想在该方法中增加指针吗?我建议你切换到'std :: string'来获得更清晰的功能和更简单的代码。 – Nonanon

回答

0

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

您的参数数组语法不正确:你错过了类型在声明数组元素的,和你错过了这两个元素,并在该“数组语法”定义。

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

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

...

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

不幸的是,我仍然得到同样的错误“tempPostCode []尚未声明”。 –

+0

您也有返回类型不匹配。 'postCodeCompare'被声明为'void'并被定义为'int'。这是[**一个工作示例**](http://melpon.org/wandbox/permlink/sUcitjYNU9HPZcuH)。请阅读C++书! –