Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-18-2002, 10:46 AM   #1 (permalink)
w00t
Totally Inept
 
w00t's Avatar
 
Join Date: Jul 2002
Location: The Great Northwest
Posts: 195
w00t is on a distinguished road
Today's exercise in stupidity...

Code:
#include <iostream.h>

class Car
{
   public:
      int itsAge;
      int getAge();
      int itsCost;
      int itsRank;
      int itsModel();
      void start();
};
  	int Car::getAge()
{
		return itsAge;
}
	int Car::itsModel()
{
	cout <<"1990 Legacy";
}

	void Car::start()
{
     cout <<"RRR..Brumbrumbrummrumbrumbroooombroom...";
}



int main()
{
     Car Subaru;
     Subaru.itsAge = 11;
     Subaru.itsCost = 2900;
     Subaru.itsRank = 2;
     cout <<"My car is a" <<Subaru.itsModel() <<"/n";
     cout <<"it cost me" <<Subaru.itsCost <<"/n";
     cout <<"It's ranked #" <<Subaru.itsRank <<"/n";
     cout <<"Man, I love this car./n";

     return 0;
};
Error: line 20:
function should return a value.
__________________
Office Space:
Best Movie.
Ever.

Contrary to popular belief, the true function of a programmer
is to turn coffee into source code.
w00t is offline   Reply With Quote
Old 08-18-2002, 11:32 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
Quote:
int Car::itsModel()
{
cout <<"1990 Legacy";
}
The int type indicats it should return a value. Either set it to return 0; or set it to be a void type function.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-20-2002, 06:08 AM   #3 (permalink)
kenshi
Registered User
 
kenshi's Avatar
 
Join Date: Jun 2002
Location: Antarctica
Posts: 43
kenshi is on a distinguished road
Send a message via ICQ to kenshi
You have a bit of a mess there. First of all, like redhead said, you have a function of int type that returns nothing. You either need to make it return something or declare it void. Second, the function is going to be called from the cout line, and the cout line within the function will be performed, which is desired, but the return value of the function will also be printed, which is not. I think the easiest way to change it up is to make Car::itsModel() return a char pointer that points to a string holding the model name. Do something like:

Code:
char* Car::itsModel()
{
  char[] model = "1990 Legacy";
  return model;
}
kenshi is offline   Reply With Quote
Old 08-20-2002, 08:38 AM   #4 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
If you are going to return a local variable then you must declare it static or it can be destroyed before you ever actually use it, like this:
Code:
char *Car::itsModel()
{
  static char[] model = "2002 Camarro SS +35";
  return model;
}
Even better would to be to throw out char* altogether and start using std::string, it's immensly more powerful and much much safer.
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote
Old 08-22-2002, 04:09 AM   #5 (permalink)
kenshi
Registered User
 
kenshi's Avatar
 
Join Date: Jun 2002
Location: Antarctica
Posts: 43
kenshi is on a distinguished road
Send a message via ICQ to kenshi
Good point.
kenshi is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Today's random poll w00t Lounge 6 08-25-2002 12:39 AM


All times are GMT -8. The time now is 05:59 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting