Thread: Classes linking error

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    22

    Classes linking error

    Hi, I am doing my first classes work (C++) and I've come up with this:

    Code:
    #include <iostream>
    #include <string>
    #include "item1.h"
    using namespace std;
    
    StockItem::StockItem()
    : m_Name(), m_InStock(0), m_Price(0), m_Distributor(), m_UPC()
    {
    }
    
    StockItem::StockItem(string Name, short InStock,
    short Price, string Distributor, string UPC)
    : m_Name(Name), m_InStock(InStock), m_Price(Price),
    m_Distributor(Distributor), m_UPC(UPC)
    {
    }
    
    void StockItem::Display()
    {
    cout << "Name: ";
    cout << m_Name << endl;
    cout << "Number in stock: ";
    cout << m_InStock << endl;
    cout << "Price: ";
    cout << m_Price << endl;
    cout << "Distributor: ";
    cout << m_Distributor << endl;
    cout << "UPC: ";
    cout << m_UPC << endl;
    }
    and saved it here:
    C:\Documents and Settings\Chris\My Documents\My game\Lobsters\classes\item1.cpp

    And saved this:

    Code:
    class StockItem
    {
    public:
    StockItem();
    
    StockItem(std::string Name, short InStock, short Price,
    std::string Distributor, std::string UPC);
    
    void Display();
    
    private:
    short m_InStock;
    short m_Price;
    std::string m_Name;
    std::string m_Distributor;
    std::string m_UPC;
    };
    here:
    C:\Documents and Settings\Chris\My Documents\My game\Lobsters\classes\item1.h

    But it comes with these errors

    3 C:\Documents and Settings\Chris\My Documents\My game\Lobsters\classes\item1.cpp In file included from C:/Documents and Settings/Chris/My Documents/My game/Lobsters/classes/item1.cpp

    [Linker error] undefined reference to `WinMain@16'

    What's wrong, I'm using Dev c++ by the way.

    Thanks

  2. #2
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Isn't it because of the lack of
    Code:
    int main()
    {
    ...
    }
    or perhaps because you get an WinMain error, it looks like your trying to create a Windows Program??
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    I don't think that's the problem, it's a linking error, someone told me on another board (The GameFAQ Programming board) that they are sometimes really hard to figure out the error.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    It's because you don't have a main just read your error how hard is it to do?
    Code:
    [Linker error] undefined reference to `WinMain@16'
    Let's play the definition game.

    Linker - Something that links your code with libaries to create the executable.

    Error - Something is wrong.

    undefined reference - you are calling a function the you have not defined yet(created).

    'WinMain@16" the name of the function you forgot to define.

    Anyways, when you create classes you still need to define main(). main has to be the entry point of your program. every single one of your programs will have main in it(in some form or another).
    Woop?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    Oh, right. Well this compiles but it still comes up with this error

    3 C:\Documents and Settings\Chris\My Documents\My game\Lobsters\classes\item1.cpp In file included from C:/Documents and Settings/Chris/My Documents/My game/Lobsters/classes/item1.cpp

    Which I think isn't good, and this is the thing that compiles:

    Code:
    #include <iostream>
    #include <string>
    #include "item1.h"
    using namespace std;
    
    StockItem::StockItem()
    : m_Name(), m_InStock(0), m_Price(0), m_Distributor(), m_UPC()
    {
    }
    
    StockItem::StockItem(string Name, short InStock,
    short Price, string Distributor, string UPC)
    : m_Name(Name), m_InStock(InStock), m_Price(Price),
    m_Distributor(Distributor), m_UPC(UPC)
    {
    }
    void StockItem::Display()
    {
    cout << "Name: ";
    cout << m_Name << endl;
    cout << "Number in stock: ";
    cout << m_InStock << endl;
    cout << "Price: ";
    cout << m_Price << endl;
    cout << "Distributor: ";
    cout << m_Distributor << endl;
    cout << "UPC: ";
    cout << m_UPC << endl;
    }
    int main() {
    }
    Thanks

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    Please help! I am desperate to keep going with my C++ tuts but I can't if I don't get this, please help

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    You need to tell Dev-CPP to compile it as a library, not a program. If you don't, it will try to link it with the main c++ lib, which requires a main (or winmain if its a "windows" project)
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    Ok, thanks, I'll try that.

    Edit:
    How do I actually get it to say something from the class? I really don't get this, can someone please give me an example of a class? I've not seen a simple one that actually works!
    Last edited by ballmonkey; 03-05-2005 at 05:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM