简体   繁体   中英

Undefined reference to a function when linking with g++

I am trying to link several object files and I am getting 3 undefined reference to a function errors.

inputtest.cpp

//test of input methods

#include <iostream>
#include <string>
#include <fstream>
#include <string>
#include <vector>


#include "Vector.h"
#include "Particle.h"
#include "read_particle_input.h"
#include "User_input.h"
#include "Particle_vector.h"

using namespace std;
using namespace berger_DEM;

int main() {

    Particle_vector particles;
    User_input input_data;

    read_particle_input(particles, input_data);

    cout <<endl<< particles.getpart(1).rho()<<endl<<particles.getpart(1).radius()<<endl;

    return 0;
}

I am linking it with 5 object files with the names found in the ".h" files(just.o files compiled from.cpp files of the same name). One of the errors I receive is

undefined reference to 'berger_DEM::read_particle_input(berger_DEM::Particle_vector&, berger_DEM::User_input&)

note that this function has a prototype in read_particle_input.h and is implemented in read_particle_input.o. I am also getting two other undefined references, but I believe they are the same issue and if i can solve this one, the same solution should work for those two.

prototype:

namespace berger_DEM
{   
    void read_particle_input(Particle_vector&,User_input&);
}

implementation: //method to read in particle_input.dat

void read_particle_input(Particle_vector & particles, User_input & input_data) 
{
    //define local variables
    Vector velocity_in;
    Vector position_in;
    double radius_in;
    double rho_in;


    //open file and define file pointer
    std::ifstream particle_input ("particle_input.dat");

    //go through each line in particle_input.dat and define each particle
    if (particle_input.is_open()) {
        for(int i=0;i<input_data.num_particles();i++) {
            //read one line from particle_input.dat
            particle_input >> position_in;
            particle_input >> radius_in;
            particle_input >> rho_in;
            particle_input >> velocity_in;

            //set particle properties and position/velocity
            particles.getpart(i).setrho(rho_in);
            particles.getpart(i).setradius(radius_in);
            particles.getpart(i).move(position_in);
            particles.getpart(i).accelerate(velocity_in);
        }
    }
    //close file
    particle_input.close();
}

I've been looking all over for someone who had the same issue, but I was unable to find anything. Thanks for the help.

Your implementation is missing the class scope.

change

void read_particle_input(Particle_vector & particles, User_input & input_data) 
{
    //define local variables

to

void berger_DEM::read_particle_input(Particle_vector & particles, User_input & input_data) 
{
    //define local variables

In implementation file it should be defined as,

void berger_DEM::read_particle_input(Particle_vector& particles, User_input& input_data);
{//  ^^^^^^^^^^^ specify that 'read_particle_input' is part of the namespace
  //...
}

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