1

I'm using code block in linux. When I try to build my cpp file where class Position is define, it gives an error-

in function '_start':
undefined reference to 'main'

Even after object file has been created.

All this files are in the same folder

This is the cpp file where Position is define

#include "POSITION.h"

std::string Position::pos(int num) {
    switch(num) {
        case 0: return "\0";
        break;
        case 1: return "st";
        break;
        case 2: return "nd";
        break;
        case 3: return "rd";
        break;
        default: return "th";
        break;
    }
}

This is the header file

#pragma once
#include <iostream>

class Position
{
public:
    std::string pos(int num);
};
Ridom Paul
  • 11
  • 2
  • 1
    You need to have a `main` function in one of the cpp files you're linking with. Unless you're building a library. – interjay Nov 17 '19 at 15:07

2 Answers2

2

Every C or C++ program starts at the main function, e.g.

int main(int argc, char **argv)
{
    // do something
    return 0;
}

Since your program has only a Position class and no main, the linker complains about that fact.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
1

The issue is that all C++ programs need an entry point. The compiler is looking for int main(), but it does not exist, so the program can't be compiled linked. If you want you could just add something like int main() { return 0; } to the end of your program if all you're trying to do is make sure it builds properly.

Nobozarb
  • 344
  • 1
  • 12