Xcode4 Xcode错误:架构x86_64的未定义符号

Xcode4 Xcode错误:架构x86_64的未定义符号,xcode4,clang,x86-64,Xcode4,Clang,X86 64,我正在为学校作业编写模板矩阵课程。我已经包括了错误以及下面的源文件和头文件。我不认为我的代码有任何问题,我很确定由于某种原因xcode无法识别头文件和源文件。tempmlated matrix类在matrix.h中声明,函数定义在matrix.cpp中,主例程在main.cpp中。我已经包括了所有三个文件以及下面Xcode给我的完整错误消息。我花了两天时间在谷歌上搜索这个错误,但都没有用。我对xcode非常陌生,因此非常感谢您的帮助。如果您需要我发布更多信息,如我的设置或其他任何信息,请随时询问

我正在为学校作业编写模板矩阵课程。我已经包括了错误以及下面的源文件和头文件。我不认为我的代码有任何问题,我很确定由于某种原因xcode无法识别头文件和源文件。tempmlated matrix类在matrix.h中声明,函数定义在matrix.cpp中,主例程在main.cpp中。我已经包括了所有三个文件以及下面Xcode给我的完整错误消息。我花了两天时间在谷歌上搜索这个错误,但都没有用。我对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;
}
Ld/Users/Mikey/Library/Developer/Xcode/DerivedData/hw3 glwfyunpxiggvvpfacidenbjyprfa/Build/Products/Debug/hw3 normal x86_64
cd/Users/Mikey/Desktop/programming/PIC10B/hw3
setenv MACOSX_部署_目标10.8
/Applications/Xcode.app/Contents/Developer/toolschains/XcodeDefault.xtoolschain/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 glwyunpxiggvvpfacidnbejyprfa/Build/Products/Debug-F/Users/Mikey/Library/Developer/Xcode/DerivedData/hw3 glwfyunpxiggvvpfacidenbejyprfa/Build/Products/Debug-filelist/Users/Mikey/Library/Developer/Xcode/DerivedData/hw3 glwfyunpxiggvvpfacidenbejyprfa/Build/hw3.Build/Objects-normal/x86_64/hw3.LinkFileList-mmacox版本min=10.8-stdlib=libc++-o/Users/Mikey/Library/Developer/Xcode/DerivedData/hw3 glwfyunpxiggvvpfacidenbjyprfa/Build/Products/Debug/hw3
架构x86_64的未定义符号:
“运算符^(矩阵,int)”,引用自:
_主音中的主音
“运算符>>(std::_1::basic_istream&,Matrix&)”,引用自:
_主音中的主音
ld:找不到架构x86_64的符号
叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用)`
//main.cpp
#包括
#包括
#包括
#包括“矩阵h”
使用名称空间std;
int main(){
行星数;
int步;
弦行星;
矢量飞机名称;
矩阵马尔可夫矩阵;
矩阵阶跃矩阵;
飞机数量;

cout我相信您使用的库是错误的。请将生成设置更改为使用:Apple LLVM编译器设置中的编译器默认值,甚至可能是libstdc++

以及:


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

如果您正在创建C程序或C++ ++ /p>
如果C++用C++或XCODE项目类型= C++来编译,而不是C.</P>我以前把它设置成LBC+++。我试过这两个选项,我仍然有同样的错误,认为它是简单的。当我回到我的房子,我会启动MacBook,看看我是否能跟踪它。Haha,我认为你是对的,只是简单,只是不知道它是什么。谢谢。Holyprin,你能帮我解决吗?不幸的是,我似乎不能重现这个问题。我可以告诉你,这是一个特定的库的链接问题,这是我能告诉你的。-我也不在MAC环境中做大量的C++,除非我在Objtovi-C项目中需要一个特定的C++函数。.:-(请注意,您的函数签名不匹配:即,类定义中的
运算符^(常数矩阵右,整数幂)
(其中
常数
不做任何事情,顺便说一句),而:
运算符^(常数矩阵&右,整数幂)
在函数模板实现中。谢谢,我更改了。但是,我仍然收到相同的错误。两个月后-你找到解决方案了吗?我有完全相同的问题。xcode无法链接。它正确编译了我的代码,如果我也包含cpp文件,错误就会消失(因为它不需要链接,因为所有符号都在main.cpp文件中定义)但这不是一个解决方案。