调用一个函数

时间:2011-01-20 21:50:52

标签: c++ function call

对不起,这一定是有史以来最愚蠢的问题之一,特别是因为我已经调用了一个函数。我已经调用了一个带有一个返回值的函数,并将返回值设置为等于变量,但使用另一个返回2个变量的函数;我只想运行该函数并返回值。

我的声明:

string diagraph ( string mono1, string mono2);

调用函数:

cout << diagraph (mono1,mono2);

功能本身:

string diagraph(string mono1, string mono2) {
    string encoded1,encoded2;
    int a,b,c,d,e,f;
    a = 0;
    b = 0;
    while( mono1 != cipherarray[b][c]){
        b++;
        if (b == 5) {
            a = 0;
            b++;
        }
    }
    a = c;
    b = d;


    a = 0;
    b = 0;

    while (mono2 != cipherarray[b][c]){ 
        b++;
        if (b == 5) {
            a = 0;
            b++;
        }
    }

    a = e;
    b = f;
}

错误(与调用函数有关):

C++\expected constructor, destructor, or type conversion before '<<' token 
 expected `,' or `;' before '<<' token 

函数未完成但会返回2个字符串

5 个答案:

答案 0 :(得分:1)

检查上面 cout << diagraph (mono1,mono2);上面的代码行,以确保没有错过任何尾随的分号,或者打开括号。

答案 1 :(得分:0)

首先,我没有在该函数中看到单个return语句。其次,您无法从函数返回两个值。您可以返回单个字符串(正如您的函数定义所说的那样),也可以修改传入的值(只要它们是引用或指针)。

编辑:详细说明

如果要修改传入的值,则需要将它们作为引用或指针。这是因为C ++中的默认行为是通过值(复制)传递参数,因此对函数外部的任何更改都不会从函数外部看到。但是,如果参数是引用/指针,您可以改变它们已经指向的内容(或指针指针,如果您想要更改原始指针指向的内容,即不是变异,而是指向新值/对象)

答案 2 :(得分:0)

尝试编译和运行代码只会给出关于非void语句结束的警告。

我建议至少添加占位符返回值,直到函数完成,类似return "";

答案 3 :(得分:0)

似乎不喜欢'cout',你是否包含命名空间std? 另外,请在使用之前检查是否声明了该功能。

答案 4 :(得分:0)

完整代码

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<limits>

using namespace std;
string getletter(string f = "q", string g = "q", string h = "q", string i = "q", string j = "q", string k = "q", string l = "q" ); 
 string diagraph ( string mono1, string mono2);



char type[81];
char filename[20];
char key [20];
string f = "q";
string g = "q";
string h = "q";
string i = "q";
string j = "q";
string k = "q";
string l = "q";
string mono1; 
string mono2;

int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int m = 0;

string cipherarray[5][5]= {
{"a","f","k","p","v"},
{"b","g","l","r","w"},
{"c","h","m","s","x"},
{"d","i","n","t","y"},
{"e","j","o","u","z"}

};
int main(){









cout<<"Enter the name of a file you want to create.\n";

cin>>filename;

cout<<"enter your codeword(codeword can have no repeating letters)\n"; 

cin>>key;

while (key[a] != '\0' ){

while(b <= 4){
        m++;
cipherarray[b][c] = key[a];

 if (m == 1 ) {
f = cipherarray[b][c];
}

 if ( m == 2 ) {
g = cipherarray[b][c];
}

 if ( m == 3 )
 {
h = cipherarray[b][c];
}

 if ( m == 4  )
 {
i = cipherarray[b][c];
}


 if ( m == 5 ) 
{
j = cipherarray[b][c];
}

 if ( m == 6 )
 {
k = cipherarray[b][c];
}

 if ( m == 7 )
 {
l = cipherarray[b][c];
}


a++;
b++;
if (key[a] == 0)
break; 
}

if (key[a] != 0){
c++;
b = 0;
}
}


// code to copy alphabet from getletter function onto cipherarray array
while ( c <= 4) {
while ( b <= 4) {
 cipherarray[b][c] = getletter(f,g,h,i,j,k,l);
 b++;     
}
b = 0;
c++;
} 









// code to display cipher array onscreen

b = 0;
c = 0;
cout<<endl<<endl<<"                      ";

string getletter(string f, string g , string h  , string i  , string j , string k , string l ) {
string letter;
string cipherarraytemplate[5][5]= {
{"a","f","k","p","v"},
{"b","g","l","r","w"},
{"c","h","m","s","x"},
{"d","i","n","t","y"},
{"e","j","o","u","z"}
};





while (cipherarraytemplate[d][e] == f || cipherarraytemplate[d][e] == g || cipherarraytemplate[d][e] == h || cipherarraytemplate[d][e] == i ||
 cipherarraytemplate[d][e] == j || cipherarraytemplate[d][e] == k || cipherarraytemplate[d][e] == l){ 
 d++; 
 if (d == 5){
e++;
d = 0;
}
 } 


letter = cipherarraytemplate[d][e];

d++;
if (d == 5){
e++;
d = 0;
}
return letter;
}

string diagraph(string mono1, string mono2) {
string encoded1,encoded2;
int a,b,c,d,e,f;
a = 0;
b = 0;
while( mono1 != cipherarray[b][c]){
b++;
if (b == 5) {
a = 0;
b++;
}
}
a = c;
b = d;


a = 0;
b = 0;

while (mono2 != cipherarray[b][c]){ 
b++;
if (b == 5) {
a = 0;
b++;
}
}

a = e;
b = f;
return "";
}