简体   繁体   中英

Undefined symbols for architecture x86_64: Compiling in C++

Working on a C++ program using chains and having trouble compiling without really knowing why.

The error I get is:

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

If I use g++ -c main2.cpp, it compiles, but I want to make sure I'm not just covering something up with that option.

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");
}

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

    }
}

Any idea why I'm getting these errors? The IOcode file was supplied to me, the chain.cpp I created. Sorry for the poor code, new to C++.

You are just compiling one file.

This should be ok for a quick and dirty try :

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

although I would add -Wall -Wextra -pedantic -Werror

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM