对"打印(字符常量 (*) [80]、整数、整数)"的未定义引用 collect2:错误:ld 返回 1 个退出状态



有人知道我的代码出了什么问题吗?我一直收到一个未定义的引用错误,我不知道是什么原因造成的。在我看来,一切都是正确的。我不太擅长使用指针和引用,但我检查了我的课本,一切似乎都写得很好。

//System Libraries Here
#include <iostream>//cin,cout
#include <cstring> //strlen(),strcmp(),strcpy()
using namespace std;
//User Libraries Here
//Global Constants Only, No Global Variables
//Allowed like PI, e, Gravity, conversions, array dimensions necessary
const int COLMAX=80;//Only 20 required, and 1 for null terminator
//Function Prototypes Here
int read(char [][COLMAX],int *);//Outputs row and columns detected from input
void sort(char [][COLMAX],int,int);//Sort by row
void print(const char [][COLMAX],int,int);//Print the sorted 2-D array
//Program Execution Begins Here
int main(int argc, char** argv) {
//Declare all Variables Here
const int ROW=30;             //Only 20 required
char array[ROW][COLMAX];      //Bigger than necessary 
int colIn,colDet,rowIn,rowDet;//Row, Col input and detected

//Input the size of the array you are sorting
cout<<"Read in a 2 dimensional array of characters and sort by Row"<<endl;
cout<<"Input the number of rows <= 20"<<endl;
cin>>rowIn;
cout<<"Input the maximum number of columns <=20"<<endl;
cin>>colIn;

//Now read in the array of characters and determine it's size
rowDet=rowIn;
cout<<"Now input the array."<<endl;
colDet=read(array, &rowDet);

//Compare the size input vs. size detected and sort if same
//Else output different size
if(rowDet==rowIn&&colDet==colIn){
sort(array,rowIn,colIn);
cout<<"The Sorted Array"<<endl;
print(array,rowIn,colIn);
}else{
if(rowDet!=rowIn)
cout<<(rowDet<rowIn?"Row Input size less than specified.":
"Row Input size greater than specified.")<<endl;
if(colDet!=colIn)
cout<<(colDet<colIn?"Column Input size less than specified.":
"Column Input size greater than specified.")<<endl;
}

//Exit
return 0;
}
int read(char array[][COLMAX], int *rowDet)
{
int i;
int rows = 0;
int cols = 0;
string input;
int len;

for(i = 0; i < *rowDet; i++)
{
cout << "Enter string: ";
cin >> input;
len = input.length();
//insert into array if and only if len is positive and less than the Max length
if (len > 0 && len < COLMAX)
{
strcpy(array[rows], input.c_str());
rows++;
if(len > cols)
{
cols = len;
}
}
else
{
cout << "Error Occured: Input String length should be > 0 and < " << COLMAX << endl;
}
}
*rowDet = rows;
return cols;
}
void sort(char array[][COLMAX],int row, int col)
{
int i;
int j;
char temp[COLMAX];

for(i = 0; i < row; i++)
{
for(j = 0; j < row - 1; j++)
{
//string comparison on two strings
//if first is greater than the second then swap them
if(strcmp(array[j], array[j+1]) > 0)
{
strcpy(temp, array[j]);
strcpy(array[j], array[j + 1]);
strcpy(array[j + 1], temp);
}
}
}
}
void print(char array[][COLMAX], int row, int col)
{
int i;
//print the strings array
for(i = 0; i < row; i++)
{
printf("%s ", array[i]);
}
}

您的打印函数原型与下面的打印函数定义不匹配;一个有const数组参数,另一个没有。只要在函数定义中包含const就可以了。

最新更新