通过引用C++获得一个奇怪的错误

Getting a weird error with pass by reference C++

本文关键字:一个 错误 引用 C++      更新时间:2023-10-16

我收到了这个奇怪的错误。当我搜索更多关于它的信息时,它说函数没有定义,但我在代码的底部定义了它。希望有人能帮助

Ld /Users/Bartski/Library/Developer/Xcode/DerivedData/areaProject-gfmvmiwbtmgqwggbbwbylymlgbvo/Build/Products/Debug/areaProject normal x86_64
cd "/Users/Bartski/Desktop/School/Fall 2014/CIS 265/areaProject"
export MACOSX_DEPLOYMENT_TARGET=10.10
/Applications/Xcode6-Beta7.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode6-Beta7.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/Bartski/Library/Developer/Xcode/DerivedData/areaProject-gfmvmiwbtmgqwggbbwbylymlgbvo/Build/Products/Debug -F/Users/Bartski/Library/Developer/Xcode/DerivedData/areaProject-gfmvmiwbtmgqwggbbwbylymlgbvo/Build/Products/Debug -filelist /Users/Bartski/Library/Developer/Xcode/DerivedData/areaProject-gfmvmiwbtmgqwggbbwbylymlgbvo/Build/Intermediates/areaProject.build/Debug/areaProject.build/Objects-normal/x86_64/areaProject.LinkFileList -mmacosx-version-min=10.10 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/Bartski/Library/Developer/Xcode/DerivedData/areaProject-gfmvmiwbtmgqwggbbwbylymlgbvo/Build/Intermediates/areaProject.build/Debug/areaProject.build/Objects-normal/x86_64/areaProject_dependency_info.dat -o /Users/Bartski/Library/Developer/Xcode/DerivedData/areaProject-gfmvmiwbtmgqwggbbwbylymlgbvo/Build/Products/Debug/areaProject
Undefined symbols for architecture x86_64:
  "circle_area(float&)", referenced from:
      _main in main.o
  "square_area(float&)", referenced from:
      _main in main.o
     (maybe you meant: __Z11square_areaRfS_)
  "rec_area(float&, float&)", referenced from:
      _main in main.o
     (maybe you meant: __Z8rec_areaRfS_S_)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

源代码:

#include <iostream>
#include <stdlib.h>
using namespace std;
// prototypes of functions
void input_function(float&);
void input_function2(float&, float&);
void output_function(float);
void circle_area(float&);
void square_area(float&);
void rec_area(float&, float&);
int main(int argc, const char* argv[]) {
    // initialization of variables
    float input_one;
    float input_two;
    float area;
    int choice;
    char repeat;
    do {
        cout << "please choose which shape you would like to use, 1 for "
                "circle, 2 for square, 3 for rectangle" << endl;
        cin >> choice;
        // user chooses which area to calculate
        switch (choice) {
            case 1:
                cout << "Please enter the radius of the circle" << endl;
                input_function(input_one);
                circle_area(input_one);
                cout << "The area of the circle is " << area << endl;
                break;
            case 2:
                cout << "Please enter the lenght of the square side" << endl;
                input_function(input_one);
                square_area(input_one);
                cout << "The area of the square is " << area << endl;
                break;
            case 3:
                cout << "Please enter the lenght of the rectangle height"
                     << endl;
                input_function2(input_one, input_two);
                rec_area(input_one, input_two);
                break;
        }
        cout << "would you like to do it again?  y/n " << endl;
        cin >> repeat;
    } while (repeat ==
             'y'); // repeats if user puts y, ends if anything else is entered
    return 0;
}
// this function gets the input from the user
void input_function2(float& input_one, float& input_two) {
    cout << "please enter the lenght and the width of the rectangle";
    cin >> input_one;
    cin >> input_two;
}
void input_function(float& input_one) {
    cin >> input_one;
    while (input_one < 0) {
        cout << "the dimension cannot be less then 0, please enter again"
             << endl;
        cin >> input_one;
    }
}
// this function calculates the area of the circle
void circle_area(float& input_one, float& area) {
    float const pi = 3.14159;
    area = input_one * input_one * pi;
}
// this function calculates the area of the square
void square_area(float& input_one, float& area) {
    area = input_one * input_one;
}
// this function calculates the area of the rectangle
void rec_area(float& input_one, float& input_two, float& area) {
    area = input_one * input_two;
}
// this function outputs the area to the console
void output_function(float& area) { cout << area; }

您声明您的函数使用一个参数,但随后定义它们使用两个参数。对于编译器来说,这是两个不同的函数,因此链接器无法解析引用,因此出现错误:

将所有不匹配的声明更改为使用两个参数而不是一个参数。

相关文章: