1

I've multiple classes for a program and I've fixed all the errors, but when I try to compile, it is now showing undefined reference to `event::event(std::__cxx11::basic_string<char, std::char_traits, std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator >, int, int)'

I'm new to cpp and not able to make sense of what's happening here. The same error is also being reported for other classes. Could it be something is wrong in the h files. I'm pasting one below, can someone tell me how to fix this.

#ifndef EVENT_H
#define EVENT_H

#include <string>
#include <iostream>
#include <iomanip>

class event
{
protected:
    std::string location, uniqueFeature;
    double uniFeaFactorT, uniFeaFactorF;

public:
    event(std::string location = "", std::string uniqueFeature = "", int uniFeaFactorT = 0, int uniFeaFactorF = 0);
    double getUFF(bool f);
    std::string getLoc();
    std::string getUF();
};

double event::getUFF(bool f){ return (f) ? uniFeaFactorT : uniFeaFactorF;}

std::string event::getLoc(){ return location;}

std::string event::getUF(){ return uniqueFeature;}

static std::ostream &operator<<(std::ostream &out, event &e)
{
    out << "City: " << e.getLoc() << "\nUnique Feature: " << e.getUF();
}

#endif

The objects are being made in the main function by reading from a file. I'm also pasting the file reading code below:

std::ifstream fin1(eventFileNm);
std::string lineV;
std::vector<string> v;
while (getline(fin1, lineV))
{
    std::istringstream iss(lineV);
    std::string fieldV;
    while (getline(iss, fieldV, ';'))
    {
        v.push_back(fieldV);
    }

    event *b = new event(v[0], v[1], stoi(v[2]), stoi(v[3]));
    eventvec.push_back(b);
}

The eventvec vector is declared as a parameter in the function.

wakanada
  • 115
  • 1
  • 9
  • In which source file do you define (implement) the constructor function? Do you build (more importantly *link*) with that file? And don't define (implement) function in your header-file. – Some programmer dude Oct 22 '20 at 08:43
  • compiler is not able to find the definition probably. See if constructor is defined with proper name and compiled as well. – Shivam Kumar Oct 22 '20 at 08:47

1 Answers1

0

The problem is not the .h probably you don't link the right .o or library that implement this function, or you don't have the implementation of the function, or simply don't compile the .cpp

An “Undefined Reference” error occurs when we have a reference to object name (class, function, variable, etc.) in our program and the linker cannot find its definition when it tries to search for it in all the linked object files and libraries.

Thus when the linker cannot find the definition of a linked object, it issues an “undefined reference” error. As clear from definition, this error occurs in the later stages of the linking process. There are various reasons that cause an “undefined reference” error.

Some possible reason(more frequent):

#1) No Definition Provided For Object

This is the simplest reason for causing an “undefined reference” error. The programmer has simply forgotten to define the object.

#2) Wrong Definition (signatures don’t match) Of Objects Used

Yet another cause for “undefined reference” error is when we specify wrong definitions. We use any object in our program and its definition is something different.

Consider the following C++ program. Here we have made a call to func1 (). Its prototype is int func1 (). But its definition does not match with its prototype. As we see, the definition of the function contains a parameter to the function.

Thus when the program is compiled, the compilation is successful because of the prototype and function call match. But when the linker is trying to link the function call with its definition, it finds the problem and issues the error as “undefined reference”.

#3) Object Files Not Linked Properly

This issue can also give rise to the “undefined reference” error. Here, we may have more than one source files and we might compile them independently. When this is done, the objects are not linked properly and it results in “undefined reference”.

Consider the following two C++ programs. In the first file, we make use of the “print ()” function which is defined in the second file. When we compile these files separately, the first file gives “undefined reference” for the print function, while the second file gives “undefined reference” for the main function.

#4) Wrong Project Type

When we specify wrong project types in C++ IDEs like the visual studio and try to do things that the project does not expect, then, we get “undefined reference”.

#5) No Library

If a programmer has not specified the library path properly or completely forgotten to specify it, then we get an “undefined reference” for all the references the program uses from the library.

#6) Dependent Files Are Not Compiled

A programmer has to ensure that we compile all the dependencies of the project beforehand so that when we compile the project, the compiler finds all the dependencies and compiles successfully. If any of the dependencies are missing then the compiler gives “undefined reference”.

Apart from the causes discussed above, the “undefined reference” error can occur in many other situations. But the bottom line is that the programmer has got the things wrong and in order to prevent this error they should be corrected.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35