Question:
How do I resolve these c++ compiler errors?
cclarinet123
14 years ago
Hello, I'm learning some C++ and using XCode, but I'm getting some compiler errors that I can't find anything useful on. This code below is a class that's included in my main as Pen.h

I get two errors: "Unknown type name string" when I declare the class attribute "Brand" and in the declaration of the argument in the WriteOnPaper function prototype.

Also, I get a "expected ; after top level declarator" after the full declaration of the WriteOnPaper function.

#include

enum Color {blue, red, black, clear};
enum PenStyle {ballpoint, felt_tip, fountain_pen};

class Pen{

public:
Color inkColor;
Color shellColor;
Color capColor;
PenStyle style;
float length;
string Brand;
int inkLevelPercent;
void WriteOnPaper(string words);
void break_in_half();
void run_out_of_ink();

};

void WriteOnPaper(string words){
if(inkLevelPercent<=0){

cout<<"Oops! Out of ink"<
}
else{

cout< inkLevelPercent-=words.length();

}

}
void break_in_half(){

inkLevelPercent/=2;
length/=2;

}
void run_out_of_ink(){

inkLevelPercent=0;

}

any suggestions would be fantastic.

Thanks!
Three answers:
Ratchetr
14 years ago
When you defined the class, you included 3 functions: WriteOnPaper, break_in_half and run_out_of_ink.



So far, so good.



But when you implement those functions, you have to tell the compiler what class the function goes with. Suppose you had 2 classes with a WriteOnPaper function, Pen and Pencil. When the compiler sees this:

void WriteOnPaper(string words){...}



How would it know which WriteOnPaper function this was? Is if for the Pen class, or the Pencil class?



The solution is simple, you just have to tell the compiler by 'qualifying' the name:

void Pen::WriteOnPaper(string words){...}



Now the compiler knows that this is the WriteOnPaper function for the Pen class.
8 years ago
Overloading unary operators (C++ in elementary terms) You overload a unary operator with the two a nonstatic member function that has no parameters, or a nonmember function that has one parameter. think a unary operator @ is talked approximately as with the assertion @t, the place t is an merchandise of form T. A nonstatic member function that overloads this operator could have right here variety: return_type operator@() A nonmember function that overloads the comparable operator could have right here variety: return_type operator@(T) An overloaded unary operator could return any form. right here occasion overloads the ! operator: #incorporate iostream utilising namespace std; struct X { }; void operator!(X) { cout << "void operator!(X)" << endl; } struct Y { void operator!() { cout << "void Y::operator!()" << endl; } }; struct Z { }; int significant() { X ox; Y oy; Z oz.; !ox; !oy; // !oz.; } right it relatively is the output of the above occasion: void operator!(X) void Y::operator!() The operator function call !ox is interpreted as operator!(X). the call !oy is interpreted as Y::operator!(). (The compiler would not enable !oz..because of the fact the ! operator has no longer been defined for sophistication Z.)
peteams
14 years ago
You either need the line:



using namespace std;



at the top of the code, or you need to refer to string as



std::string


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...