0

I'm working on a program:

#include <iostream>
#include <vector>
#include <utility> //std::pair, std::make_pair

using namespace std;

class User { 
    private: 
        size_t userIndex;
        vector< pair<int,int> > ratings;

    public:
        void addRating(int movieIndex, int rating) {
            ratings.push_back( make_pair(movieIndex, rating) );
        }
};

However, when I compile it with g++ I get the following error:

/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../lib/libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-1.7.28-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain'
/usr/src/debug/cygwin-1.7.28-1/winsup/cygwin/lib/libcmain.c:39:(.text.startup+0x7e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
collect2: error: ld returned 1 exit status

Interestingly, when I comment out the following line, this error goes away:

    //vector< pair<int,int> > ratings;

Why is this happening when this line is uncommented?

Bob Shannon
  • 638
  • 2
  • 10
  • 19
  • 8
    Seems you're not defining a `main()` somewhere? – Mark Nunberg Apr 29 '14 at 04:19
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Michael Burr Apr 29 '14 at 08:16
  • When you comment the line, the code becomes invalid and the compilation should result in an error. On an unrelated note, your setup is very old. You probably want to uninstall cygwin, download msys2, and install gcc from there. – n. m. could be an AI Jul 22 '22 at 19:47

1 Answers1

0

The full given code compiles fine with:

g++ -c user.cpp

The problem indicated by the linker is that you are telling g++ to create an executable, not a library, but there is no main function specified:

g++ user.cpp

Gives the error you mention about WinMain being missing (under windows).

This is entirely unrelated to the line you indicate commenting. However, it may be that the real problem is inside a Makefile; try touch'ing the code file instead and see if that causes the problem as well. If your Makefile says to recompile cpp files but incorrectly without the -c flag, you will get this behaviour. For example, try removing the -c flag from the Makefile below:

%.o: %.cpp %.h
    g++ -c %.cpp
TamaMcGlinn
  • 2,840
  • 23
  • 34