[英]When i compile i keep getting build failed? I am using Xcode
I don't know why this won't work.我不知道为什么这行不通。 I don't know if it have to do with passing though the function or the way I created my variables.
我不知道这是否与通过函数或我创建变量的方式有关。 But I have to use the
toDigit
function for my homework.但是我必须使用
toDigit
函数来做作业。
#include <iostream>
#include <fstream>
using namespace std;
int toDigit(char &);
int main(){
int number;
char isbn;
ifstream inputFile;
inputFile.open("numbers.txt");
while (inputFile >> isbn) {
number = toDigit(isbn);
cout << number << endl;
}
inputFile.close();
return 0;
}
int toDigit(char ch){
return ch - '0';
}
Initially, you declare toDigit to be最初,您声明 toDigit 为
int toDigit(char &);
which would be read as this: a function returning an int that takes one argument that is a reference to a char.这将被解读为:一个返回一个 int 的函数,该函数接受一个参数,该参数是一个对char的引用。
Then, later you define this function like this:然后,稍后您可以像这样定义这个函数:
int toDigit(char ch){
return ch - '0';
}
which could be read like this: a function returning an int that takes one argument that is a char.可以这样读:一个函数返回一个 int ,它接受一个 char 参数。
Do you see the difference?你看得到差别吗? Hint: a reference .
提示:参考。
In c++, definitions need to be the same as declarations, so you need to make them the same.在 C++ 中,定义需要与声明相同,因此您需要使它们相同。
EITHER:任何一个:
Make the declaration作出声明
int toDigit(char );
// ^^ no &
OR或者
make the definition做出定义
int toDigit(char & ch){
// ^^ & added
return ch - '0';
}
In this case, the first option makes more sense because you don't need read access the the character and chars are efficient to copy, so a copy would be fine.在这种情况下,第一个选项更有意义,因为您不需要读取访问字符和字符可以有效复制,因此复制就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.