0

我正在为学校作业编写模板矩阵类。我已经包含了错误以及下面的源文件和头文件。我认为我的代码没有任何问题,我很确定由于某种原因 xcode 无法识别头文件和源文件。模板化矩阵类在 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;
}
4

2 回答 2

2

我相信您正在使用错误的库进行编译。更改要使用的构建设置:Apple LLVM 编译器设置中的编译器默认值甚至可能是 libstdc++。

和:

项目 -> 构建设置 -> 查找 LLVM 编译器组 -> C++ 标准库

于 2013-02-16T05:56:34.657 回答
0

如果您正在创建 C 程序或 C++?

如果是 C++,则使用 G++ 或 Xcode 项目 TYPE = C++,而不是 C 构建。

于 2013-06-30T20:15:01.867 回答