简体   繁体   English

当我编译时,我不断得到构建失败? 我正在使用 Xcode

[英]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.

相关问题 我正在尝试在xcode中编译一个名为supertux的开源游戏,但我一直在找不到文件错误 - I am trying to compile in xcode an open source game called supertux but I keep getting file not found errors 我正在通过 Xcode 运行此代码,但由于错误导致构建失败 - I am running this code through Xcode and i am getting build failed with the error 为什么我在尝试编译时遇到此编译错误? - Why am i getting this compile error when i try to compile? 为什么我总是收到错误消息或构建错误消息? - WHy am I keep getting an error message or build error message? 为什么我在使用&这时会出错? - Why am I getting an error when using &this? 为什么会出现此模板编译错误? - Why am i getting this template compile error? 当我尝试在启用推理引擎的情况下编译OpenCv时,出现错误 - When I try to compile OpenCv with inference engine enabled, I am getting an error 在XCode上使用I / O Kit中的方法时,构建失败 - Build fails when using methods from I/O Kit on XCode 我一直收到语法错误,但我不确定原因 - I keep getting a syntax error but I am not sure why 如何使用Xcode随附的带有命令行工具的外部库来编译和构建程序? - How can I compile and build a program using external library with command line tools come with Xcode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM