-
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
-
Isn't it because of the lack of
or perhaps because you get an WinMain error, it looks like your trying to create a Windows Program??
-
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.
-
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).
-
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
-
Please help! I am desperate to keep going with my C++ tuts but I can't if I don't get this, please help
-
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)
-
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!