gpt4 book ai didi

c++ - Xcode 错误 : Undefined symbols for architecture x86_44 & ld: symbol(s) not found for architecture x86_64

转载 作者:行者123 更新时间:2023-11-28 06:31:41 25 4
gpt4 key购买 nike

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

这是我的全部代码:

#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/macintosh/Library/Developer/Xcode/DerivedData/project_5_new-fwiwizttrvaiwxesfsfyyahdetpv/Build/Products/Debug/project\ 5\ new

Undefined symbols for architecture x86_64: "isMagicSquare(int, int, int)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

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

最佳答案

您的代码的问题在于您声明带有签名的 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 错误 : Undefined symbols for architecture x86_44 & ld: symbol(s) not found for architecture x86_64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27417736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com