Can't figure out why my class is failing epically.

This is my main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;
#include "randomclassheader.h"
#include <string.h>
#include <cstdlib>
int main()
{
    char firstname[50];
    char secondname[50];
    char eyecolour[50];
    int age;
    float weight;

    Person(firstname, secondname, eyecolour, age, weight);

    cout << "Please enter your first name:" << endl;
    cin >> firstname;

    cout << "Please enter your second name:";
    cin >> secondname;

    cout << "Please enter your eye colour:";
    cin >> eyecolour;

    cout << "Please enter your age:";
    cin >> age;

    cout << "Please enter your weight:";
    cin >> weight;


    cout << "This is your first name: " << firstname;

    cout << endl << endl << endl << endl << "This is the name of the first person in the class: "  <<endl;



    return 0;
}



and here is my person.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 #include <iostream>
using namespace std;
#include "randomclassheader.h"
#include <string.h>




Person::Person(char firstName[], char secondName[] ,char EyeColour[] ,int Age, float weight)
{
	strcpy(firstname, firstName);
	return firstName;

}



void Person::createFirstName(char createFirstName[])
{
    strcpy(firstname, createFirstName);
    return firstName;
}


void Person::createSecondName(char createSecondName[])
{
    strcpy(secondname, createSecondName);
    return secondName;
}


void Person::createWeight(float createWeight[])
{
    weight == Weight;
    return Weight;
}


void Person::createAge(char createAge[])
{
    age == Age;
    return Age;
}


void Person::createEyeColoure(char createEyeColour[])
{
    strcpy(eyecolour, createEyeColoure);
    return EyeColour;
}


Oh and the errors I'm getting are here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
||=== Random test class, Debug ===|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp||In constructor ‘Person::Person(char*, char*, char*, int, float)’:|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|11|error: ‘firstname’ was not declared in this scope|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|12|error: returning a value from a constructor|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp||In member function ‘void Person::createFirstName(char*)’:|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|20|error: ‘firstname’ was not declared in this scope|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|21|error: return-statement with a value, in function returning 'void'|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp||In member function ‘void Person::createSecondName(char*)’:|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|27|error: ‘secondname’ was not declared in this scope|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|28|error: return-statement with a value, in function returning 'void'|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|32|error: prototype forvoid Person::createWeight(float*)’ does not match any in class ‘Person’|
/home/rej3kt/Desktop/random test class/Random test class/randomclassheader.h|32|error: candidate is: void Person::createWeight(float)|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|39|error: prototype forvoid Person::createAge(char*)’ does not match any in class ‘Person’|
/home/rej3kt/Desktop/random test class/Random test class/randomclassheader.h|26|error: candidate is: void Person::createAge(int)|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp||In member function ‘void Person::createEyeColoure(char*)’:|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|48|error: ‘eyecolour’ was not declared in this scope|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|49|error: return-statement with a value, in function returning 'void'|
||=== Build finished: 12 errors, 0 warnings ===|


I've been at this for ages so a quick response would be awesome, also if you could respond in layman's terms I'd appreciate it! Thanks -Rob


Just say if you need me to post my header file as well
Last edited on
Hello rej3kt,


Well, like the error says:
1
2
3
4
5
6
Person::Person(char firstName[], char secondName[] ,char EyeColour[] ,int Age, float weight)
{
	strcpy(firstname, firstName); // <--- 'firstname' is obviously not declared in your class
	return firstName; // <--- It's a constructor you can't return anything

}
an so on
error: ‘firstname’ was not declared in this scope|
Maybe a case mismatch, or something. Post your header for more info.

error: returning a value from a constructor|
error: return-statement with a value, in function returning 'void'|
What the error says. Constructors and void functions can't return anything. You can have an empty return statement in a void function though.

error: prototype for ‘void Person::createWeight(float*)’ does not match any in class ‘Person’|
etc.
In c++ function is defined not only by its name, but also return type and arguments. Therefore void Person::createWeight(float*) is not the same as void Person::createWeight(float)
Last edited on
If that's true, about the not being able to return anything then why in my working version did I get this working:

Different variables and stoof, it's one I made earlier then tried to recreate to get an understanding of it.

1
2
3
4
5
6
7
8
9
10
Person::Person()
{
}
Person::Person(char theName[],float theWeight,int theAge, char theEyeColour[])
{
	strcpy(personName, theName);
	personWeight = theWeight;
	personAge = theAge;
	strcpy(personEyeColour, theEyeColour);
}


Also Hamsterman in my working version I have this:
1
2
3
4
5
6
7
8
void Person::setName(char theName[])
{
	strcpy(personName, theName);
}
char* Person::getName()
{
	return personName;
}



I understand now void doesn't return anything it's just copying the string from the variable personName to the member in the class called theName, so what does char* Person::getName() do? This is my working version btw
This is my header file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef RANDOMCLASSHEADER_H_INCLUDED
#define RANDOMCLASSHEADER_H_INCLUDED



#endif // RANDOMCLASSHEADER_H_INCLUDED





class Person
{



 public:
    Person(char firstName[], char secondName[], char EyeColour[],int Age, float Weight);

    void createFirstName(char firstName[]);
    char* showFirstName();

    void createSecondName(char secondName[]);
    char* showSecondName();

    void createAge(int Age);
    int showAge();

    void createEyeColoure(char EyeColour[]);
    char* showEyeColour();

    void createWeight(float Weight);
    float showWeight();


 private:
    char firstName[50];
    char secondName[50];
    char EyeColour[50];
    int Age;
    float Weight;
};
your first attempt worked because you use the variables you declared. you cannot use variables you didn't declare
Topic archived. No new replies allowed.