Xcode错误:架构x86_44&的未定义符号ld:找不到架构x86_64的符号

时间:2014-12-11 07:47:55

标签: c++

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

这是我的整个代码:

#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 / Users / macintosh / Library / Developer / Xcode / DerivedData / project_5_new -fwiwizttrvaiwxesfsfyyahdetpv / Build / Products / Debug / project \ 5 \ new normal x86_64       cd“/ Users / macintosh / Desktop / project 5 new”       export 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_5_new-fwiwizttrvaiwxesfsfyyahdetpv / Build / Products / Debug -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 / project \ 5 \ new。 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查看调用)

我尝试了许多尝试解决问题的方法,但没有找到解决办法。请帮忙!

1 个答案:

答案 0 :(得分:1)

您的代码存在的问题是您声明了带有签名的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;  
}