简体   繁体   English

奇怪的编译错误C ++“未定义引用”和“重定位被截断”

[英]odd compilation error c++ “undefined reference” & “relocation truncated”

here is my code it is very simple in the end all it is going to do is prompt the user to input the size of two 2d arrays and then input the entries and multiple the result as if they were mathmatical matrix. 这是我的代码,最后,要做的就是提示用户输入两个2d数组的大小,然后输入条目并将结果乘以数学矩阵,将它们简单化。 the program is far from done but i like to compile thesesmall programs as i go to see if the all parts are working. 该程序远未完成,但是我喜欢编译这些小程序,因为我要去看看所有部分是否都在工作。 when i compile this code i get this odd error. 当我编译这段代码时,我得到这个奇怪的错误。 note i am very much a beginner when i comes to C++ as well as coding in general. 请注意,我一般来说是C ++和编码方面的初学者。

$ g++ matrix.C -omatrixs -lm
/tmp/ccUDYTb1.o:matrix.C:(.text+0x266): undefined reference to `Matrixsettings(int, int, int, int)'
/tmp/ccUDYTb1.o:matrix.C:(.text+0x266): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Matrixsettings(int, int, int, int)'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.1/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccUDYTb1.o: bad reloc address 0x1c in section `.xdata'
collect2: error: ld returned 1 exit status

i am using cygwin 64bit with a gnu compiler and notepad++ to compile and write this. 我正在使用带有gnu编译器和notepad ++的cygwin 64bit进行编译和编写。

#include <iostream>
#include <cmath>
using namespace std;
//Prototypes
double Matrixsettings(int, int, int, int);

int main()
{
    int a=2, b=2, c=2, d=2;
    int m1 [a] [b], m2 [c] [d];
    cout<<  Matrixsettings( a,b,c,d);
    cout<< a<<b<<c<<d;
return 0;
}

double Matrixsettings( int &a, int &b,  int &c, int &d)
{ 
cout<< "how many rows in the first matrix: ";
cin>> a;
cout<< "how many columns in the first matrix: ";
cin>> b;
cout<< "how many rows in the second matrix: ";
cin>> c;
cout<< "how many columns in the second matrix: ";
cin>> d;
return 0;
}

This is incorrect: 这是不正确的:

double Matrixsettings(int, int, int, int);

The correct prototype for the actual function you have is: 您拥有的实际功能的正确原型是:

double Matrixsettings(int&, int&, int&, int&);

You want to fix the prototype, and not the function, because your function actually writes to its parameters a , b , c , and d . 您要修复原型,而不要修复函数,因为函数实际上会写入其参数abcd

The error message indicates that the compiler made a call to the function you declared in the prototype (since it matched the arguments you gave), but the linker couldn't find a function that matched the name Matrixsettings but took four int arguments (not int& ). 错误消息表明编译器调用了您在原型中声明的函数(因为它与给定的参数匹配),但是链接器找不到与名称Matrixsettings匹配但使用了四个int参数的函数(不是int& )。

The "truncated relocation" error was just a consequence of the first error—a cascade error of sorts. “截断的重定位”错误只是第一个错误的结果-各种级联错误。 Fixing the first error will fix both errors. 修正第一个错误将同时修正这两个错误。

First you say Matrixsettings takes four integers, then you say it takes four references to integers. 首先,您说Matrixsettings接受四个整数,然后说对四个整数进行引用。 You need to chose one and stick with it. 您需要选择一个并坚持下去。

Change: 更改:

double Matrixsettings(int, int, int, int);

to: 至:

double Matrixsettings(int&, int&, int&, int&);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM