Errors in my program

in compiling my program, I had several errors that I have tried to find.
Can anyone help with this program.

#include <iostream>
using namespace std;

class MyString
{
private:
char* Buffer;

public:
MyString(const char* InitialInput)
{
if(InitialInput != NULL)
{
Buffer = new char [strlen(InitialInput)+1];
strcpy(Buffer, InitialInput);
}
else
Buffer = NULL;
}
MyString(const MyString& CopySource)
{
Buffer = new char [strlen(CopySource.Buffer)+1];
strcpy(Buffer, CopySource.Buffer);
}
else
Buffer = NUll;
}
~MyString()
{
if (Buffer != NULL)
delete [] = Buffer;
}
int GetLength(){
return strleng(Buffer);
}
const char* GetString(){
return Buffer;
}
};
class Human
{
private:
int Age;
bool Gender;
MyString Name;

public:
Human(const MyString& InputName, int InputAge,bool InputGender):
Name(InputName),Age(InputAge),Gender(InputGender){}

int GetAge (){
return Age;
}
};
int main() {
Mystring FirstMan("Adam");
MyString FirstWoman("Eve");

cout<<"sizeof(Mystring) = "<< sizeof(MyString)<<endl;
cout<<"sizeof(FirstMan) = "<< size(FirstMan)<<endl;
cout<<"sizeof(FirstWoman) = "<< sizeof(FirstWoman)<<endl;

Human FirstMaleHuman(FirstMan, 25, true);
Human FirstFemaleHuman(FirstWoman,18, false);

cout<<"sizeof(Human) = "<< sizeof(Human)<<endl;
cout<<"sizeof(FirstMaleHuman)= "<<sizeof(FirstMaleHuman)<<endl;
cout<<"sizeof(FirstFemaleHuman)= "<<sizeof(FirstFemaleHuman)<<endl;
return 0;
}

Below are my errors
_______________________________________________________
prog.cpp:25:2: error: expected unqualified-id before ‘else’
else
^
prog.cpp:27:12: error: expected ‘;’ after class definition
}
^
prog.cpp: In constructor ‘MyString::MyString(const char*)’:
prog.cpp:14:43: error: ‘strlen’ was not declared in this scope
Buffer = new char [strlen(InitialInput)+1];
^
prog.cpp:15:32: error: ‘strcpy’ was not declared in this scope
strcpy(Buffer, InitialInput);
^
prog.cpp: In copy constructor ‘MyString::MyString(const MyString&)’:
prog.cpp:22:46: error: ‘strlen’ was not declared in this scope
Buffer = new char [strlen(CopySource.Buffer)+1];
^
prog.cpp:23:35: error: ‘strcpy’ was not declared in this scope
strcpy(Buffer, CopySource.Buffer);
^
prog.cpp: At global scope:
prog.cpp:28:22: error: declaration of ‘~MyString’ as non-member
~MyString()
^
prog.cpp: In function ‘int GetLength()’:
prog.cpp:34:28: error: ‘Buffer’ was not declared in this scope
return strleng(Buffer);
^
prog.cpp:34:34: error: ‘strleng’ was not declared in this scope
return strleng(Buffer);
^
prog.cpp: In function ‘const char* GetString()’:
prog.cpp:37:20: error: ‘Buffer’ was not declared in this scope
return Buffer;
^
prog.cpp: At global scope:
prog.cpp:39:1: error: expected declaration before ‘}’ token
};
^
prog.cpp: In function ‘int GetLength()’:
prog.cpp:35:12: warning: control reaches end of non-void function [-Wreturn-type]
}
^
prog.cpp: In function ‘const char* GetString()’:
prog.cpp:38:12: warning: control reaches end of non-void function [-Wreturn-type]
}
^
Last edited on
Post your error messages.
Have you tried to understand the compiler error messages? They tell you what is wrong.

Line 25: You have an else statement, but it's not associated with an if statement.

Lines 26-27 are extraneous.

Line 31: Lose the =

Line 34: strlen misspelled.

Line 56: MyString misspelled.

Line 60: sizeof misspelled.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/

Last edited on
I am not sure which compiler you are using, but if you double click the error, it will take you to the line with the error. Sometimes its not exactly correct, but usually it is. I know that visual studio 2013 professional will do this. But as Abstraction has mentioned, you have to read the errors and make sense of them. They do tell you what is wrong. They may not tell you how to fix them, but you can usually figure out why your getting the error based on what it says.
Topic archived. No new replies allowed.