Sign in to follow this  
TraderJack

big trouble with code

Recommended Posts

Ugh, I've been working though all these compiler errors for so long now. Think I could get some help? It's an excersise using a simple string class. Here is the header file:
#ifndef __MYSTRING_H__
#define __MYSTRING_H__

class MyString
{
public:
	MyString( );							//Blank constructer
	MyString(const Mystring& otherMystring);				//Copy Constructer
	MyString(const char *phrase);				//Constructer with constant string
	~MyString( );	

	char reverse();							//Reverse method
	void print();							//Print Method
};

#endif

And here is the accuall program:
#include <iostream>
#include <string>

#include "Mystring.h"								//Header File

using namespace std;

MyString::MyString()
{
	cout << "Default Constructor initiated:" <<endl;
}

MyString::MyString(const MyString& otherMyString)
{
	cout << "Copy Constructor initiated:" <<endl;
}

MyString::MyString(const char *phrase)
{
	cout << "Constructor with string initiated:" <<endl;
}

MyString::~MyString()
{
	cout << "Destruction commencing" <<endl;

MyString::reverse()
{
	reverse(phrase.begin(),phrase.end());
}

MyString::print()
{
	cout << phrase <<endl;
}

int main(int argc,char **argv)
{

	//
	//	These test cases should work
	//

	MyString str;
	str.reverse();
	str.print(); //this one should print a blank string

	MyString str2("Test String");
	str2.reverse();
	str2.print();

	MyString str3 = str2;
	str3.reverse();
	str3.print();

return 0;
}

Anyone wanna take a swat at it? -TJ [Edit: I added the [source] tag for you. - Oluseyi]

Share this post


Link to post
Share on other sites
Without your post including the compiler errors and I am going to assume that you have posted the entire source. If so, it looks like you are missing a member variable "phrase" and are never assigning anything in your constructors either.

Share this post


Link to post
Share on other sites
With the highlighting, it's obvious that your destructor has no closing brace. But I suspect that's just a typo for when you were posting here.

So what errors do you get? Error messages give you information as to what is wrong with the code. If you don't tell us the error message, we can only take shots in the dark.

One more thing. Each error has a number, and that number has corresponding documentation. For instance, with MSVC, compiler errors begin with "C", eg C3851. If you're using MSVC and you have MSDN installed locally, highlight the error in the Output window and press F1. Otherwise, enter the code over at MSDN Online.

Share this post


Link to post
Share on other sites
Right then, fixed a couple of things, and here's the next stuff and a list of errors.

Header:

#ifndef __MYSTRING_H__
#define __MYSTRING_H__

class MyString
{
public:
MyString( ); //Blank constructer
MyString(const MyString& otherMyString); //Copy Constructer
MyString(const char *phrase); //Constructer with constant string
~MyString( );

void backwards(); //Reverse method
void print(); //Print Method
const char *display;
};

#endif

Program Code:

#include <iostream>
#include <string>

#include "Mystring.h" //Header File

using namespace std;

MyString::MyString()
{
cout << "Default Constructor initiated:" <<endl;
}

MyString::MyString(const MyString& otherMyString)
{
cout << "Copy Constructor initiated:" <<endl;
}

MyString::MyString(const char *phrase)
{
cout << "Constructor with string initiated:" <<endl;
display = phrase;
}

MyString::~MyString()
{
cout << "Destruction commencing" <<endl;
}

MyString::backwards()
{
reverse(display.begin(),display.end());
}

MyString::print()
{
cout << display <<endl;
}

int main(int argc,char **argv)
{

//
// These test cases should work
//

MyString str;
str.backwards();
str.print(); //this one should print a blank string

MyString str2("Test String");
str2.backwards();
str2.print();

MyString str3 = str2;
str3.backwards();
str3.print();

return 0;
}

And a rough breakdown of the errors since I cannot ctrl+copy them:

Line 33- Error C2556- "int MyString::backwards(void)" overloaded function. Only differs from return type from "void MyString::backwards(void)" (see decleration of MyString::backwards)
Line 33- Error C2371- "MyString::backwards" redefinition, different basic types (see decleration of MyString::backwards)
Line 34- Error C2228- left of ".begin" must have class/struc/union type type is const char *
Line 34- Error C2228- left of ".end" must have class/struc/union type type is const char *
Line 34- Error C3861- "reverse" identifier not found, even with arguement-indipendant lookup
Line 38- Error C2556- "int MyString::print(void)" overloaded function. Only differs from return type from "void MyString::print(void)" (see decleration of MyString::print)
Line 38- Error C2371- "MyString::print" redefinition, different basic types (see decleration of MyString::print)
Line 50- Error C2264- "MyString::backward" error in function definition or decleration, function not called
Line 51- Error C2264- "MyString::print" error in function definition or decleration, function not called
Line 54- Error C2264- "MyString::backward" error in function definition or decleration, function not called
Line 55- Error C2264- "MyString::print" error in function definition or decleration, function not called
Line 58- Error C2264- "MyString::backward" error in function definition or decleration, function not called
Line 59- Error C2264- "MyString::print" error in function definition or decleration, function not called

Using Visual C++ Toolkit 2003.

Thanks again guys.

-IV

Share this post


Link to post
Share on other sites
well, there are quite a number of things wrong, but thats cool. first, in your constructor you have to initialize the data member to a default values, otherwise you'll get large errors when you try to operate on it. second, in your class function definitions, you have to include the return type. so if you have :

class SomeClass {
public:
void Myfunc();
};
// ---- in the .cpp file you'd write
void SomeClass::Myfunc()
{
/* does something cool
}

also, the data type of the class data member should probably be std::string. and in your function backwards, reverse in an undefined function, as are begin() and end(). furthermore, the '=' operator is undefined for your specific class.
good luck.

Share this post


Link to post
Share on other sites
Oh, constructive criticism I see. Thank god! I'll write it down in my little diary... "Oluseyi in a number of words told me to go **** myself..." Thank god the staff member that handles programming newbies is so patient. (End sarcasm here)

I am learning. Duh.

Anyhow, as for stormrunner. Thank you graciously. Your advice helped me push through all of the problems in the program. It now runs perfectly. The program I'm using to learn C++ can be so lacking sometimes I have to outsource for alot of the neccessary skills.

Reverse, begin(), and end() are functions by the way. They are defined in <algorithm>. std::string was infact the way to go, and I feel stupid for leaving out those return types.

Thanks again.

-TJ

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Seriously, why the hell does the staff always bitch at people? The point of a forum like this is for people to help others, not yell at them for having problems. And this is the beginner's forum - if you're just going to be an asshole and criticize people who post beginner questions here, then why do you even read this forum? You do this a lot, and it's really unfortunate that gamedev can't find someone better to be in charge of a job you obviously don't care anything about.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Just learn C++. It'll save us both time, because you honestly haven't a clue what you're doing, and until you do it's a waste of our time to correct your code. There's no point treating symptoms of a malaise.


yo Oluseyi, tone it down man, its not like he's abusing the forumns, he's posting in the "For Beginners" and he's obviously trying to learn.

to the AP, grow some testicals (even if you are female) i dispise people who critize behind a mask.

PS. Besides, Oluseyi can see your IP.

Share this post


Link to post
Share on other sites
You have created a member variable of type const char* which means that the only way you can assign it's value is through constructor initialiser lists like so:

MyString::MyString() : phrase("Foo") //legal
{
}

whereas direct assignment to a const variable is illegal

MyString::MyString()
{
phrase = "Foo"; //not allowed
}

You should also note that once the phrase variable has been assigned in the constructor you can't change it's value.

Another problem you have is that you are trying to call begin() and end() methods on phrase - these are methods of the STL container classes like vector, you can't call them on a const char* variable.

Instead what you could do is to declare phrase as either a char* - in which case you are required to manually allocate and delete the memory for the string via new[] and delete[] (array of characters), or declare it as a char array. Then to assign a string to it you could use something like memcpy() with the length the passed string (plus one for the null terminator) multiplied by sizeof(char) as the number of bytes to copy.

To reverse the string you will could use a simple for() loop to iterate backwards through the string and copy it character by character to a second, temp string, before assigning this temp sting back into your original.

EDIT: your also gonna want to assign the string inside your constructors otherwise nothing will happen when you call print().

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
And this is the beginner's forum - if you're just going to be an asshole and criticize people who post beginner questions here, then why do you even read this forum?
Because a ton of "encouraging" folk (perhaps like you) teach the wrong thing, compounding the problem and making it even more difficult for future beginners to learn the right thing. And once that's done, they end up a few months or years down the line asking advanced questions with absolutely no idea what's going on.

For instance, this fellow really has no idea what a constructor and destructor are for, how they work, what a copy constructor is and how it differs from an assignment operator, how C simulates strings using null-terminated character arrays, how that differs from C++'s std::string... This guy probably shouldn't even be using C++ yet, given its complexity. But no, some "encouraging" moron always says "Start with C++. I did, and I'm doing fine three months later." As always, conveniently skipping the fact that he, the advisor, is not writing correct C++ and is therefore worthless as a comparison.

It gets on my nerves. It particularly gets on my nerves because I've been saying this for about two years now. C is not a high-level language per se. C is not a good beginner's language (neither is Java). Unless you are already conditioned to deal with complexity or non-intuitive rules, your first few months of C will be a blind haze, and it doesn't help that most references, tutorials and textbooks suck. In fact, this fellow is suffering from textbook-itis, in which concepts are introduced but not sufficiently explained, resulting in chaos.

Quote:
Original post by silvermace
yo Oluseyi, tone it down man, its not like he's abusing the forumns, he's posting in the "For Beginners" and he's obviously trying to learn.
True. He's not trying to learn the right things, though.

But allow me to break it down for all of you:

class MyString
{
public:
...
const char *display;
};
Why const? Why public?
MyString::backwards()
{
reverse(display.begin(),display.end());
}
Since when did char have methods? And if it did, wouldn't they be accessed via a pointer using the pointer-to-method dispatch operator, ->? What is reverse? Where is it declared/defined and what does it do?

These are not simple, syntactic errors. These are indicative of a lack of understanding of forward references, visibility and data hiding, type, dispatch... and this is ignoring all the smaller errors. This is someone going beyond what he knows, creating a two-tier, compounded problem in that he doesn't know his tools, so he can't solve his problem.

Call a spade a spade, people. The kid needs to learn C++ before he can get anything else to work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this