我正在创建一个代码,以允许用户创建2D数组的大小,然后输入数字以填充每个数组空间,然后让它检查是否是学校项目的魔方,但我仍然收到上面的两个错误。

这是我的整个代码:

#include <iostream>

bool isMagicSquare(int row, int col, int magicSquare);

using namespace std;

int main()
{
int row, col;
int input = 0;
cout << "Enter the number of rows: ";
cin >> row;
cout << endl;
cout << "Enter the number of columns: ";
cin >> col;
cout << endl;

int magicSquare[row][col];

if (col != row)
{
    cout << "Sorry, this cannot be a magic square." << endl;
    cout << "Enter the number of rows: ";
    cin >> row;
    cout << endl;
    cout << "Enter the number of columns: ";
    cin >> col;
    cout << endl;
}
else if ( col == row )
{
    for ( int i = 0; i < row; i++)
    {
        for ( int j = 0; j < col; j++)
        {
            cout << "Enter a number for row " << i+1 << " column " << j+1 << ": ";
            cin >> input;
            magicSquare[i][j] = input;
        }
    }

}

isMagicSquare(row, col, magicSquare[row][col]);
{
    if (isMagicSquare(row, col, magicSquare[row][col]) == true)
    {
        cout << "This is a magic square." << endl;
    }
    if (isMagicSquare(row, col, magicSquare[row][col]) == false)
    {
        cout << "This is not a magic square." << endl;
    }

}

return 0;
}

bool isMagicSquare(int row, int col, int magicSquare[row][col])
{
int rowTotal, colTotal, diagA, diagB;
rowTotal = 0;
colTotal = 0;
diagA = 0;
diagB = 0;

for ( int r = 0; r < row; r++)
{
    rowTotal = 0;
    for ( int c = 0; c < col; c++)
    {
        rowTotal += magicSquare[r][c];
    }

}
for ( int c = 0; c < col; c++)
{
    colTotal = 0;
    for ( int r = 0; r < row; r++)
    {
        colTotal += magicSquare[ r ][ c ];
    }
}
for ( int r = 0; r < row; r++)
{
    diagA += magicSquare[r][r];
}
for ( int i = 0; i < row; i++)
{
    diagB += magicSquare[i][i];
}
if (rowTotal != colTotal)
{
    return false;
}
else if (diagA != rowTotal)
{
    return false;
}
else if (diagB != rowTotal)
{
    return false;
}

return true;
}


这是完整的错误消息:


  ld /用户/ macintosh /库/开发人员/ Xcode / DerivedData / project_5_new-fwiwizttrvaiwxesfsfyyahdetpv / Build / Products / Debug / project \ 5 \新普通x86_64
      cd“ / Users / macintosh / Desktop / project 5 new”
      导出MACOSX_DEPLOYMENT_TARGET = 10.10
      /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10。 10.sdk -L / Users / macintosh / Library / Developer / Xcode / DerivedData / project_5_new-fwiwizttrvaiwxesfsfyyahdetpv / Build / Products / Debug -F / Users / macintosh / Library / Developer / Xcode / DerivedData / project_5znew-ff调试-filelist / Users / macintosh / Library / Developer / Xcode / DerivedData / project_5_new-fwiwizttrvaiwxesfsfyyahdetpv / Build / Intermediates / project \ 5 \ new.build/Debug/project \ 5 \ new.build/Objects-normal/x86_64/project \ 5 \ new.LinkFileList -mmacosx-version-min = 10.10 -stdlib = libc ++ -Xlinker -dependency_info -Xlinker / Users / macintosh / Library / Developer / Xcode / DerivedData / project_5_new-fwiwizttrvaiwxesfsfyyahdetpv / Build / Intermediates。 build / Debug / project \ 5 \ new.build/Objects-normal/x86_64/project \ 5 \ new_dependency_info.dat -o / Users / macint osh / Library / Developer / Xcode / DerivedData / project_5_new-fwiwizttrvaiwxesfsfyyahdetpv / Build / Products / Debug / project \ 5 \ new
  
  架构x86_64的未定义符号:
    “ isMagicSquare(int,int,int)”,引用自:
        _main在main.o中
  ld:找不到架构x86_64的符号
  clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)


我已经尝试了很多方法来尝试解决问题,但是没有找到解决方案。请帮忙!

最佳答案

您的代码存在的问题是您使用签名声明了ismagicsquare函数

bool isMagicSquare(int row, int col, int magicSquare);


但是您使用其他签名定义了函数

bool isMagicSquare(int row, int col, int[][] magicSquare);


这使第一个声明没有定义。

同样实现此目的的简单方法是

#include <iostream>



using namespace std;

bool isMagicSquare(int row, int col, int** magicSquare)
{

int rowTotal, colTotal, diagA, diagB;
rowTotal = 0;
colTotal = 0;
diagA = 0;
diagB = 0;

for ( int r = 0; r < row; r++)
{
rowTotal = 0;
for ( int c = 0; c < col; c++)
{
    rowTotal += magicSquare[r][c];
}

}
for ( int c = 0; c < col; c++)
{
colTotal = 0;
for ( int r = 0; r < row; r++)
{
    colTotal += magicSquare[ r ][ c ];
}
}
for ( int r = 0; r < row; r++)
{
diagA += magicSquare[r][r];
}
for ( int i = 0; i < row; i++)
{
diagB += magicSquare[i][i];
}
if (rowTotal != colTotal)
{
return false;
}
else if (diagA != rowTotal)
{
return false;
}
else if (diagB != rowTotal)
{
return false;
}

return true;
}
int main()
{
int row, col;
int input = 0;
cout << "Enter the number of rows: ";
cin >> row;
cout << endl;
cout << "Enter the number of columns: ";
cin >> col;
cout << endl;

int** magicSquare;

if (col != row)
{
cout << "Sorry, this cannot be a magic square." << endl;
cout << "Enter the number of rows: ";
cin >> row;
cout << endl;
cout << "Enter the number of columns: ";
cin >> col;
cout << endl;
}
else if ( col == row )
{
magicSquare=new int *[row];
for ( int i = 0; i < row; i++)
{
    magicSquare[i]=new int[col];
    for ( int j = 0; j < col; j++)
    {
        cout << "Enter a number for row " << i+1 << " column " << j+1 << ": ";
        cin >> input;
        magicSquare[i][j] = input;
    }
}

}


if (isMagicSquare(row, col, magicSquare) == true)
{
    cout << "This is a magic square." << endl;
}else
{
    cout << "This is not a magic square." << endl;
}



return 0;
}

关于c++ - Xcode错误:体系结构x86_44和ld的 undefined symbol :体系结构x86_64找不到符号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27417736/

10-11 22:53
查看更多