C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 18th, 2006, 09:54 PM
cards1007 cards1007 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 2 cards1007 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 8 sec
Reputation Power: 0
"constructors not allowed a return type" compile error, please help

I'm just getting started programming, and I'm using the book "C++: How To Program 5th Edition". I'm trying to complete one of the exercises (Chapter 3, exercise 3.13 for those that have the book...) and I'm having one compile error I can't seem to figure out.

I have the program broken up into three files, Invoice.h is the class definition, Invoice.cpp defines the member functions, and Invoice Exercise.cpp will contain the main function to create the objects and call their functions to display information.

Invoice.h compiles correctly and contains the following:
Code:
// Chapter 3 Exercise 3.13: Invoice class
// This file presents the Invoice class definition.
#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

class Invoice
{
public:
	Invoice( string, string, int, int ); // constructor that initializes data members
	void setPartNumber( string ); // function to set the part number
	string getPartNumber(); // function to get the part number
	void setPartDescription( string ); // function to set the part description
	string getPartDescription(); // function to get the part description
	void setOrderQuantity( int ); // function to set the quantity being purchased
	int getOrderQuantity(); // function to get the quantity being purchased
	void setPartPrice( int ); // function to set the price per item
	int getPartPrice(); // function to get the price per item
	int getInvoiceAmount(); // functino to get the invoice amount
private:
	string partNumber;
	string partDescription;
	int orderQuantity;
	int partPrice;
	int invoiceAmount;
}


Invoice.cpp does not compile correctly, and gives the following error:
Code:
Invoice.cpp(17) : error C2533: 'Invoice::{ctor}' : constructors not allowed a return type


I can't figure out what that error means or what I need to do to fix it. Here's the contents of Invoice.cpp:
Code:
// Chapter 3 Exercise 3.13: Invoice class
// This file contains the Invoice class member function defintions.
#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

#include "Invoice.h" // include class definition for Invoice

// constructor initializes data member values
Invoice::Invoice( string num, string desc, int quant, int price )
{
	setPartNumber( num );
	setPartDescription( desc );
	setOrderQuantity( quant );
	setPartPrice( price );
} // end Invoice constructor

// function to set part number
void Invoice::setPartNumber( string num )
{
	partNumber = num;
} // end function setPartNumber

// function to get part number
string Invoice::getPartNumber()
{
	return partNumber;
} // end function getPartNumber

// function to set part description
void Invoice::setPartDescription( string desc )
{
	partDescription = desc;
} // end function setPartDescription

// function to get part description
string Invoice::getPartDescription()
{
	return partDescription;
} // end function getPartDescription

// function to set quantity of item being purchased
void Invoice::setOrderQuantity( int quant )
{
	orderQuantity = quant;
} // end function setOrderQuantity

// function to get order quantity
int Invoice::getOrderQuantity()
{
	return orderQuantity;
} // end function getOrderQuantity

// function to set price per item
void Invoice::setPartPrice( int price )
{
	partPrice = price;
} // end function setPartPrice

// function to get part price
int Invoice::getPartPrice()
{
	return partPrice;
} // end function getPartPrice

// function to get invoice amount
int Invoice::getInvoiceAmount()
{
	invoiceAmount = 0;
	if (orderQuantity < 0)
	{
		orderQuantity = 0;
		cout << "Order quantity was not valid and has been set to 0." << endl;
	} // end if
	if (partPrice < 0)
	{
		partPrice = 0;
		cout << "Part price was not valid and has been set to 0." << endl;
	} // end if

	invoiceAmount = orderQuantity * partPrice;
	return invoiceAmount;
} // end function getInvoiceAmount


Any advice would be greatly appreciated!

Reply With Quote
  #2  
Old October 18th, 2006, 09:59 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 14
Send a message via AIM to ubergeek
Well, I was all ready to say that the error meant that constructors are not allowed a return type, but you don't seem to have violated that particular rule. The only thing I can see is you forgot a missing semicolon after the Invoice class definition.
C Code:
Original - C Code
  1.  
  2. class Invoice
  3. {
  4. ...
  5. }; // <--- semicolon after closing brace
  6.  

Then Invoice.h is included directly into Invoice.cpp, so the compiler sees the constructor definition before it finds the expected semicolon, and gets confused. Try fixing that and see what happens.
Comments on this post
Ray_BigGuy agrees: Right on!

Reply With Quote
  #3  
Old October 18th, 2006, 10:08 PM
cards1007 cards1007 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 2 cards1007 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 8 sec
Reputation Power: 0
Quote:
Originally Posted by ubergeek
Well, I was all ready to say that the error meant that constructors are not allowed a return type, but you don't seem to have violated that particular rule. The only thing I can see is you forgot a missing semicolon after the Invoice class definition.
C Code:
Original - C Code
  1.  
  2. class Invoice
  3. {
  4. ...
  5. }; // <--- semicolon after closing brace
  6.  

Then Invoice.h is included directly into Invoice.cpp, so the compiler sees the constructor definition before it finds the expected semicolon, and gets confused. Try fixing that and see what happens.


That solved it! Thanks very much for your help. I wondered if I would get some "RTFM" type responses, but you've definitely made a good first impression for these boards. Hopefully I can learn quickly so I'll be able to help others here soon.

Reply With Quote
  #4  
Old June 11th, 2012, 09:51 PM
Ray_BigGuy Ray_BigGuy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 1 Ray_BigGuy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 8 sec
Reputation Power: 0
Thanks

Just wanted to say Thanks. I had the same error and the same cause was that semi colon being missed. The error really leads you down the wrong path in this case.

Thanks again.

Reply With Quote
Closed Thread

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > "constructors not allowed a return type" compile error, please help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap