-
Help!!!!!!!!!!!!!!!!!1
So here I am trying to make a little practice (very limited) string class when all of the sudden my code won't compile, what a bi*ch huh? Well Please help.
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
// here I was just starting my very limited string class and
//all of the suddem THIS!!!!!! won't compile, help.
//limited string class, just for practice
class String
{
public:
String();
int Getlength(); //error
int Get string();
private:
int length;
int *string;
};
String::Get String() //error
{
return *string;
}
int main()
{
system("PAUSE");
return 0;
}
//I was just beginning this little round of coding, as you can see I am really a newbie, and though that creating my own string class will really help a lot since it involves a lot of what C++ it's all about.
//PS DON'T MAKE FUN OF ME, oh wait yes go ahead and laugh away
-
ok I got those fixed but now I can more errors (so this is how microsoft felt after releasing Win ME, tries to update 98 and ends up making the mess a bigger.)
-
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
//limited string class, just for practice
class String
{
public:
String();
int Getlength();
int Getstring();
private:
int length;
int *string;
};
String::Getstring()
{
return *string;
}
int main()
{
system("PAUSE");
return 0;
}
/*I get this error "31 c:\docume~1\incogn~1\desktop\limite~1.cpp
ANSI C++ forbids declaration `Getstring' with no type" */
Please anybody out there?
-
Code:
String::Getstring()
{
return *string;
}
is wrong. it should be this:
Code:
int* String::Getstring()
{
return string; /* edited for error */
}
and the declararion should be this:
int* Getstring();
I usually use strings that are of type char but that's just me.
-
Why should it have a (*) on it?
-
Oh because you're returning a reference of a string so it can be displayed? Am I close, well correct me.
-
Because that is the type you declared it as. The return value just has to match the type of whatever you are returning.
Sorry I meant:
Code:
int* String::Getstring()
{
return string;
}
the variable doesn't have to have the * just the return type.
it's late.
-
Ok, I got it to compiler now......Thanks.......I'll keep on working on it tomorrow (man if you just read books and don't practice you're screwed, I am finding this the hard way, now I am having problems creating a simple string class).
Code:
const char *String::Getstring()
{
return string;
}
So is this telling the computer to return the string as a constant reference? In turn returning the string?
-
Code:
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
//limited string class, just for practice
class String
{
public:
String();
int Getlength(); //error
char *Getstring();
private:
int length;
char *string;
};
char* String::Getstring()
{
return string;
}
int main()
{
system("PAUSE");
return 0;
}
this compiles. you're very close but still not getting a few concepts. keep it up though
-
Yeah thanks it compiled. Somehow my post telling you that I got it to compile didn't show up I guess because I see it but can't edit it.