1

使用链处理 C++ 程序,并且在不知道原因的情况下无法编译。

我得到的错误是:

Undefined symbols for architecture x86_64:
  "userInputOutput(linearList*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
      _main in ccBEoeAc.o
  "chain::chain(int)", referenced from:
      _main in ccBEoeAc.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

如果我使用 g++ -c main2.cpp,它会编译,但我想确保我不只是用那个选项来掩盖一些东西。

Main2.cpp

#include <iostream>
#include "Chain.h"
#include "IOcode.h"

using namespace std;

int main (){

    //Call constructor, initial capacity 10
    chain *myChain=new chain(10);

    //Print initial chain
    userInputOutput(myChain, "chain");
}

链.cpp

 #include "Chain.h"
#include <stdio.h>
#include <iostream>
#include <sstream>
#include "Myexception.h"

using namespace std;

    chain::chain(int initialCapacity)
    {
    cout<<"This method is working"<<endl;
/*  if (initialCapacity <1)
    {
        //throw illegalParameterValue();
    }
    firstNode=NULL;
    listSize=0;
*/
};

IOcode.cpp

#include <iostream>
#include "Linearlist.h"

using namespace std;

void userInputOutput (linearList* l, string dataStructure){

    for (int i = 0; i < 13; i++)
            l->insert(i, i+1);
    cout<<"The initial "<<dataStructure<<" is: ";
    l->traverse();
    cout<<"welcome to the assignmet 3!"<<endl;
    while(true){
       //do stuff

    }
}

知道为什么我会收到这些错误吗?提供给我的 IOcode 文件是我创建的 chain.cpp。对不起,C++ 新手,糟糕的代码。

4

1 回答 1

4

您只是在编译一个文件。

这应该可以进行快速而肮脏的尝试:

g++  main2.cpp Chain.cpp IOcode.cpp 

虽然我会补充-Wall -Wextra -pedantic -Werror

于 2013-09-27T20:32:15.297 回答