我正在为学校作业编写模板矩阵类。我已经包含了错误以及下面的源文件和头文件。我认为我的代码没有任何问题,我很确定xcode因为某些原因无法识别标题和源文件。 tempmlated矩阵类在Matrix.h中声明,函数定义在Matrix.cpp中,主例程在main.cpp中。我已经包含了所有三个文件以及Xcode在下面给出的完整错误消息。我花了最近2天谷歌搜索这个错误无济于事。我对xcode很新,所以任何帮助都非常感谢。如果您需要我发布更多信息,例如我的设置或其他任何信息,请不要犹豫。谢谢 完全错误是:
Ld /Users/Mikey/Library/Developer/Xcode/DerivedData/hw3-glwfyunpxigvvpfacidnbejyprfa/Build/Products/Debug/hw3 normal x86_64
cd /Users/Mikey/Desktop/programming/PIC10B/hw3
setenv MACOSX_DEPLOYMENT_TARGET 10.8
/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.8.sdk -L/Users/Mikey/Library/Developer/Xcode/DerivedData/hw3-glwfyunpxigvvpfacidnbejyprfa/Build/Products/Debug -F/Users/Mikey/Library/Developer/Xcode/DerivedData/hw3-glwfyunpxigvvpfacidnbejyprfa/Build/Products/Debug -filelist /Users/Mikey/Library/Developer/Xcode/DerivedData/hw3-glwfyunpxigvvpfacidnbejyprfa/Build/Intermediates/hw3.build/Debug/hw3.build/Objects-normal/x86_64/hw3.LinkFileList -mmacosx-version-min=10.8 -stdlib=libc++ -o /Users/Mikey/Library/Developer/Xcode/DerivedData/hw3-glwfyunpxigvvpfacidnbejyprfa/Build/Products/Debug/hw3
Undefined symbols for architecture x86_64:
"operator^(Matrix<double>, int)", referenced from:
_main in main.o
"operator>>(std::__1::basic_istream<char, std::__1::char_traits<char> >&, Matrix<double>&)", 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)`
//main.cpp
#include<iostream>
#include<vector>
#include<string>
#include "Matrix.h"
using namespace std;
int main () {
int numberOfPlanets;
int steps;
string planet;
vector<string> planetNames;
Matrix<double> markovMatrix;
Matrix<double> stepMatrix;
cout<<"Please enter the number of Planets:";
cin>>numberOfPlanets;
cout<<"Please enter the names of the Planets:";
for(int i=0;i<numberOfPlanets;i++) {
cin>>planet;
planetNames.push_back(planet);
}
cout<<"How many steps will you take?";
cin>>steps;
cout<<"Please enter the "<<numberOfPlanets<<"X"<<numberOfPlanets<<" Markov Matrix:";
cin>>markovMatrix;
stepMatrix=markovMatrix^steps;
for(int i=0;i<numberOfPlanets;i++) {
int maxEntry = stepMatrix.findMax(i);
cout<<"After "<<steps<<" steps, from "<<planetNames[i]<<"you are most likely to end up at " <<planetNames[maxEntry];
cout<<endl;
}
return 0;
}
//Matrix.h
#include<iostream>
#include<iomanip>
#ifndef MATRIX_H
#define MATRIX_H
using namespace std;
template<typename T>
class Matrix {
public:
Matrix();
Matrix(int r, int c);
~Matrix();
Matrix<T>(const Matrix<T>& right);
Matrix<T>& operator=(const Matrix<T>& right);
T& operator() (int i, int j);
T operator() (int i, int j) const;
friend ostream& operator<<(ostream& out, const Matrix<T>&right);
friend istream& operator>>(istream& in, Matrix<T>& right);
friend Matrix<T> operator*(const Matrix<T> left, const Matrix<T> right);
friend Matrix<T> operator+(const Matrix<T> left, const Matrix<T> right);
friend Matrix<T> operator^(const Matrix<T> right, int power);
int findMax(int r) const;
private:
int rows;
int columns;
T* elements;
};
#endif`
//Matrix.cpp
#include "Matrix.h"
#include<iostream>
#include<iomanip>
using namespace std;
template<typename T>
Matrix<T>::Matrix() {
rows = 0;
columns = 0;
elements = NULL;
}
template<typename T>
Matrix<T>::Matrix(int r, int c) {
rows = r;
columns = c;
elements = new T[rows*columns];
for(int i=0;i<rows*columns;i++) {
elements[i]=0;
}
}
template<typename T>
Matrix<T>::Matrix(const Matrix<T>& right) {
rows = right.rows;
columns = right.columns;
elements = new T[rows*columns];
for(int i=0;i<rows*columns;i++)
elements[i]=right.elements[i];
}
template<typename T>
Matrix<T>& Matrix<T>::operator=(const Matrix<T>& right) {
if(this!=&right) {
rows = right.rows;
columns = right.columns;
delete[] elements;
for(int i=0;i<rows*columns;i++) {
elements[i]=right.elements[i];
}
}
return *this;
}
template<typename T>
T Matrix<T>::operator()(int i, int j) const {
return elements[i*rows+j];
}
template<typename T>
T& Matrix<T>::operator()(int i, int j) {
return elements[i*rows+j];
}
template<typename T>
Matrix<T> operator+(const Matrix<T>& right, const Matrix<T> left) {
Matrix<T> A(right.rows, right.columns);
for(int i=0;i<A.rows;i++) {
for(int j=0;j<A.columns;j++) {
A(i,j) = right(i,j)+left(i,j);
}
}
return A;
}
template<typename T>
Matrix<T> operator*(const Matrix<T>& right, const Matrix<T> left) {
Matrix<T> A(left.rows, right.columns);
for(int i=0;i<left.rows;i++) {
for(int j=0;j<right.columns;j++) {
for(int k=0;k<right.rows;k++) {
A(i,j)+=left(i,k)*right(k,j);
}
}
}
return A;
}
template<typename T>
Matrix<T>::~Matrix() {
delete[] elements;
}
template<typename T>
Matrix<T> operator^(const Matrix<T>& right, int power) {
Matrix<T> A = right;
for(int i=1;i<power;i++)
A=A*right;
return A;
}
template<typename T>
int Matrix<T>::findMax(int r) const {
int maxColumnEntry = 0;
T rowMax = elements[r*columns];
for(int j=1;j<columns;j++) {
if(elements[r*columns+j]>rowMax) {
rowMax = elements[r*columns+j];
maxColumnEntry = j;
}
}
return maxColumnEntry;
}
template<typename T>
ostream& operator<<(ostream& out,const Matrix<T>& right) {
for(int i=0;i<right.rows*right.columns;i++) {
out<<setw(10)<<right.elements[i]<<" ";
if((i+1)%right.columns==0)
out<<endl;
}
return out;
}
template<typename T>
istream& operator<<(istream& in, Matrix<T>& right) {
for(int i=0;i<right.rows*right.columns;i++){
in>>right.elements[i];
}
return in;
}
答案 0 :(得分:2)
我相信你正在使用错误的lib进行编译。更改要使用的构建设置:Apple LLVM编译器设置中的编译器默认值甚至可能是libstdc ++。
和
项目 - &gt;构建设置 - &gt;查找LLVM编译器组 - &gt; C ++标准库
答案 1 :(得分:0)
如果您正在创建C程序或C ++?
如果是C ++,使用G ++或Xcode构建项目TYPE = C ++,而不是C。