big trouble with code

Started by
11 comments, last by darookie 19 years, 6 months ago
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]

-IVHumble Student

Advertisement
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.
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.
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

-IVHumble Student

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 writevoid 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.
- stormrunner
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.
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

-IVHumble Student

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.
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.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
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().

This topic is closed to new replies.

Advertisement