[prog] C++ class counstruct problem
Elizabeth Barham
lizzy at soggytrousers.net
Thu May 1 13:14:18 EST 2003
E! <evolution_9112003 at yahoo.com.au> writes:
> Hi I been trying to run this following program. But it gives me a weird
> error: "asemetric.cpp:16: type specifier omitted for parameter `
> Internal compiler error: Error reporting routines re-entered.
> Please submit a full bug report,
> with preprocessed source if appropriate.
> See <URL:http://bugzilla.redhat.com/bugzilla/> for instructions."
>
> Can anyone please tell me where did I go wrong in this program? I am new
> to C/ C++
Like Random said, there is an error with the main() function
definition, which normally looks like:
int main (int argc, char* argv[]) { ... }
> #include<iostream>
> using namespace std;
>
> class vehicle
> {
> public:
> vehicle (char name, char model);
You probably mean "char * name", etc
> void print();
> private:
> char car_name;
Again, "char * car_name" - its a pointer to a string of characters,
not a single character.
> char car_model;
> void testcar();
undefined function.
> };
>
> int main (int argc, char * argv[])
Corrected.
> {
> vehicle a("Toyota", "Corolla", 1990);
There is no constructor that matches this pattern. As it is
you would need to write,
vehicle a('T', 'C');
as 'T' and 'C' are both characters.
A character array (aka a string) can be represented as a character
pointer (that is, a pointer to the first character containing the
string).
> vehicle b("Nissan", "Sunny", 1994);
> vehicle c("Mercedes-Benz", "550", 1990);
>
> cout << "Original Statistics: " << endl;
> cout << "=====================" <<endl;
>
> a.print();
> b.print();
> c.print();
>
> return (0);
> }
>
> vehicle::vehicle(char name, char model)
> :car_name(name), car_model(model)
> {
> testcar();
> };
Here's a working version:
#include<iostream>
using namespace std;
class vehicle
{
public:
vehicle (char * name, char * model, int year);
void print();
private:
char * car_name;
char * car_model;
void testcar();
};
int main (int argc, char* argv[])
{
vehicle a("Toyota", "Corolla", 1990);
vehicle b("Nissan", "Sunny", 1994);
vehicle c("Mercedes-Benz", "550", 1990);
cout << "Original Statistics: " << endl;
cout << "=====================" <<endl;
a.print();
b.print();
c.print();
return (0);
}
void vehicle::testcar() {
cout << __FUNCTION__ << ":" << __LINE__ << endl;
}
vehicle::vehicle(char * name, char * model, int year)
:car_name(name), car_model(model)
{
testcar();
};
void vehicle::print() {
cout << "You are cruis'n in a " << car_name << " " << car_model << "." << endl;
}
hth, Elizabeth
More information about the Programming
mailing list