Advertisement

Help me fix this error please.

Started by June 11, 2002 10:15 PM
8 comments, last by Meister_J 22 years, 5 months ago
I just tried to write my first program without any help.. I started with 29 errors and I''m down to 3 now(woohoo!) but I can''t find out how to fix them.. maybe you all can help me. Main Code:
  #include <iostream.h>
#include <fstream.h>
#include <string.h>
#include "item2.h"


StockItem::StockItem()
: m_InStock(0), m_Price(0), m_Name(), 
  m_Distributor(), m_UPC()
{
}

StockItem::StockItem(char Name[50], short InStock, short Price, char Distributor[100], char UPC[35])
: m_InStock(InStock), m_Price(Price), m_Name(Name),  m_Distributor(Distributor), m_UPC(UPC)
{
}


void StockItem::Read(ifstream& s) 
{
	s >> m_Name;  
	s >> m_InStock;
	s >> m_Price;
	s >> m_Distributor;
	s >> m_UPC;
}

void StockItem::Display()
{
	cout << "Name:  " << m_Name << endl;
	
}

std::string;

int main()
{
	ifstream s_Inventory("inventory1.inv");
	StockItem AllItems[100];
	short i, InvCount;


	for(i=0; i<100; i++)
	{
		AllItems[i].Read(s_Inventory);
		if (s_Inventory.fail() != 0)
			break;
	}

	InvCount = i;

	for(i=0; i < InvCount; i++)
	{
		AllItems[i].Display();
	}


	return 0;
}

  
Contenents of item2.h:
  class StockItem
{
public:
    StockItem();

    

    void Display();
    void Read(ifstream& s);

private:
    short m_InStock;
    short m_Price;
    char m_Name[50];
    char m_Distributor[100];
    char m_UPC[35];
};

  
--------------------Configuration: StockItem2 - Win32 Debug-------------------- Compiling... item2.cpp C:\Program Files\Microsoft Visual Studio\MyProjects\StockItem2\item2.cpp(14) : error C2511: ''StockItem::StockItem'' : overloaded member function ''void (char [],short,short,char [],char [])'' not found in ''StockItem'' c:\program files\microsoft visual studio\myprojects\stockitem2\item2.h(2) : see declaration of ''StockItem'' C:\Program Files\Microsoft Visual Studio\MyProjects\StockItem2\item2.cpp(61) : fatal error C1004: unexpected end of file found Error executing cl.exe. -J __________________________________________________________________________________________ Don''t ever frown. You never know when someone is falling in love with your smile.
-J__________________________________________________________________________________________Don't ever frown. You never know when someone is falling in love with your smile.
I'm a newbie too, so bear with me if I'm wrong

Problem 1: It looks like you are defining a method for Stock Item, without declaring it. You have two constructor methods, but only declare one. You need to add a second declaration to the .h file.

EDIT: Just to clarify....

You are defining this:

quote: StockItem::StockItem(char Name[50], short InStock, short Price, char Distributor[100], char UPC[35]): m_InStock(InStock), m_Price(Price), m_Name(Name), m_Distributor(Distributor), m_UPC(UPC)


...without ever declaring it.

Problem 2: *shrug* Related to other problems, maybe?

Problem 3: Not sure if this is the problem, but I usually put semi colons after the end brace of each method. Try it, and see if it helps any.

Hope that helps!

[edited by - Peon on June 11, 2002 11:22:42 PM]
Peon
Advertisement
In your main code you declare a body for the function StockItem::StockItem(char Name[50], short InStock, short Price, char Distributor[100], char UPC[35]) but you never declare that function in your class definition. You need to declare that function in your class definition.

It is different than StockItem::StockItem() in case you were thinking you only needed to declare that name once. The functions are different if their name or arguments are different.

Also, you need to change this line:

std::string;

to

using std::string;


I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm
Peon - you''re right about the constructors, but there''s no need for a semicolon after the end of a method definition
Ah okay, my bad Musta seen it somewhere and picked it up; I always use semicolons at the end of methods with my code, hehe.
Peon
Ok.. those 2 problems.. now I have 2 more.. I can already tell this language is going to be a pain in the ass

New code:

  #include <iostream.h>#include <fstream.h>#include <string.h>#include "item2.h"StockItem::StockItem(): m_InStock(0), m_Price(0), m_Name(),   m_Distributor(), m_UPC(){}StockItem::StockItem(char Name[50], short InStock, short Price, char Distributor[100], char UPC[35]): m_InStock(InStock), m_Price(Price), m_Name(Name),  m_Distributor(Distributor), m_UPC(UPC){		}void StockItem::Read(ifstream& s) {	s >> m_Name;  	s >> m_InStock;	s >> m_Price;	s >> m_Distributor;	s >> m_UPC;}void StockItem::Display(){	cout << "Name:  " << m_Name << endl;	}int main(){	ifstream s_Inventory("inventory1.inv");	StockItem AllItems[100];	short i, InvCount;	for(i=0; i<100; i++)	{		AllItems[i].Read(s_Inventory);		if (s_Inventory.fail() != 0)			break;	}	InvCount = i;	for(i=0; i < InvCount; i++)	{		AllItems[i].Display();	}	return 0;};  


New Item2.h:

  class StockItem{public:    StockItem();		StockItem(char m_Name[50], short m_InStock, short m_Price, char m_Distributor[100], char m_UPC[35]);        void Display();    void Read(ifstream& s);private:    short m_InStock;    short m_Price;    char m_Name[50];    char m_Distributor[100];    char m_UPC[35];};  


New errors:
--------------------Configuration: StockItem2 - Win32 Debug--------------------
Compiling...
item2.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\StockItem2\item2.cpp(15) : error C2536: ''StockItem::m_Name'' : cannot specify explicit initializer for arrays
c:\program files\microsoft visual studio\myprojects\stockitem2\item2.h(15) : see declaration of ''m_Name''
C:\Program Files\Microsoft Visual Studio\MyProjects\StockItem2\item2.cpp(15) : error C2536: ''StockItem::m_Distributor'' : cannot specify explicit initializer for arrays
c:\program files\microsoft visual studio\myprojects\stockitem2\item2.h(16) : see declaration of ''m_Distributor''
C:\Program Files\Microsoft Visual Studio\MyProjects\StockItem2\item2.cpp(15) : error C2536: ''StockItem::m_UPC'' : cannot specify explicit initializer for arrays
c:\program files\microsoft visual studio\myprojects\stockitem2\item2.h(17) : see declaration of ''m_UPC''
Error executing cl.exe.

StockItem2.exe - 3 error(s), 0 warning(s)


How in the hell do you get strings of characters in there? ..I''m confused on that.

-J

__________________________________________________________________________________________
Don''t ever frown. You never know when someone is falling in love with your smile.
-J__________________________________________________________________________________________Don't ever frown. You never know when someone is falling in love with your smile.
Advertisement
Okay, I'll take a stab at it

What looks odd to me is this construction:

StockItem::StockItem(char Name[50], short InStock, short Price, char Distributor[100], char UPC[35]) **:** m_InStock(InStock), m_Price(Price), m_Name(Name), m_Distributor(Distributor), m_UPC(UPC)

I haven't seen anything like this before (with that extra colon I bolded), though its more likely that its just something I haven't seen. Again, I could be wrong, just a guess.

[edited by - Peon on June 11, 2002 11:41:44 PM]
Peon
Meister_J: you can't assign to arrays. Use strcpy [*]instead.<br><br>Peon: that is called member initializer lists.<br><br>
[*]or better yet, strncpy to avoid buffer overflows, or even better, std::string with which you won't have to concern yourself with such overflows at all.<br><br><SPAN CLASS=editedby>[edited by - IndirectX on June 11, 2002 11:46:35 PM]</SPAN>
---visit #directxdev on afternet <- not just for directx, despite the name
change


        StockItem(char m_Name[50], short m_InStock, short m_Price, char m_Distributor[100], char m_UPC[35]);  // to      StockItem(char Name[50], short InStock, short Price, char Distributor[100], char UPC[35])        


Your declarations much match the definitions exactly.


[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on June 11, 2002 12:48:13 AM]
[size=2]
quote: Original post by wild_pointer
Your declarations much match the definitions exactly.

Nope. Only the types are required to match exactly. Identifiers are merely syntactic sugar for your benefit. (They are required to be consistent within the definition, though).

This topic is closed to new replies.

Advertisement