Errors

I'm having some errors its saying LiveCount was a previous data member. Void alien setLiveCount was already defined, as well as getLiveCount. I'm also getting alien:class type redefinition error as well as too many initializers.







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
51
52
53
54
 #include<iostream>
#include<string>
using namespace std;

class alien 
{
	alien();
	alien(*Livecount);
	void kill();
	bool Alive();
	bool getAlive();
	int getLivecount();
	void setLiveCount(int,int,int);
private:
	static int Livecount(int,int,int);

	

	void setLiveCount(int, int, int)
	{
		int LiveCount = 3;
		
			
	}

	int getLivecount()
	{
		int Livecount = -1;
	}

};
class::alien
{
	bool Alive = 'true';
	int Livecount = 3;

};
class::alien kill()
{
	bool Alive = false;
}

int main()
{
	 char string [] = { "Alien1", "Alien2", "Alien3" };
	alien;
	
	cout <<



		system("pause");
	return 0;
} 
There are a LOT of things wrong with this code. Even if it compiled, the code doesn't do anything or at least there is no way it does what you intend.

I strongly recommend you re-read a tutorial on classes, as many of the concepts seem to be escaping you. Maybe even go back to basics -- like stuff before classes (variables, types, etc), as you are getting some other fundamental stuff wrong.

That said... see my comments:

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
51
52
53
54
55
56
57
#include<iostream>
#include<string>
using namespace std;

class alien 
{
    alien();
    alien(*Livecount);              // <- What is 'Livecount'?  You never said.  ERROR
                                    //    Also, why the '*'?  That doesn't make any sense here.   ERROR
    void kill();
    bool Alive();
    bool getAlive();
    int getLivecount();                     // <- getLivecount declared here
    void setLiveCount(int,int,int);         // <- setLiveCount declared here
private:
    static int Livecount(int,int,int);

	

    void setLiveCount(int, int, int)        // <- setLiveCount declared AGAIN here.  Multiple declarations in the same class = ERROR
    {
        int LiveCount = 3;                  // <- This creates a local variable (only visible inside this function).
                                            //    while this is not an error, it is useless because you can never use this variable.
            
    }

    int getLivecount()                      // <- getLivecount declared AGAIN here.  multiple declarations = ERROR
    {
        int Livecount = -1;                 // <- again, this does nothing.
    }                                       // <- getLivecount declared to return an int, but you do not return anything.  ERROR

};
class::alien                                // <- class keyword before scope operator (::) is nonsense.  ERROR
{                                           // <-  what are you trying to do here?  Is this supposed to be part of the 'alien' class?
                                            //    if yes, then why didn't you put it inside the alien class?
    bool Alive = 'true';                    // <- 'true' is a character literal and not a boolean value.  ERROR.  You probably meant to use true without the quotes
    int Livecount = 3;
                                            // and again.. these are all useless variables with local scope that do not do anything
};
class::alien kill()                         // <- "class::"  is nonsense.  ERROR.   Did you mean:  "void alien::kill()"  ?
{
    bool Alive = false;                     // <- again, this does nothing, as 'Alive' is a local variable not seen outside this function
}

int main()
{
     char string [] = { "Alien1", "Alien2", "Alien3" };     // <- A 'char' is a single character.  "Alien1" is not a single character, it is a string.  ERROR
                                            //  Either you want an array of strings (preferable), or you want an array of char arrays (less preferable)
    alien;                                  // <- ??? what are you trying to do here?

    cout <<                     // <-  ?? what are you trying to output?  ERROR



        system("pause");
	return 0;
} 



EDIT: replaced tabs with spaces to fix indentation
Last edited on
Yes I am confused about classes and how they work. I've looked up how they work,and I guess I'm still not understanding them fully. This program is supposed to create an alien inside the class and give it a life (I choose 3), kill the alien and cout you win, when all three are killed.
CLASS EXPLANATION


To understand classes, first, understand structs.

A struct is a collection of several different variables. The idea is they are logically linked so that you would always want to treat them as though they were in a group.

For example:

1
2
3
4
5
6
struct AlienStats
{
    std::string name;
    int life;
    int exp;
};


Notice: You declare the variables inside the struct. These are called "members", as they are members of the struct. This means that every "instance" of the struct, or every "object", will have its own copy of those members.

You create objects just like you create normal variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int foo;  // <- a normal variable named 'foo'

AlienStats  bar;  // <- an AlienStats object named 'bar'

//  since 'bar' is an object, it has its own copy of 'name', 'life', and 'exp'
//   You can access bar's members with the dot (.) operator
bar.name = "Jeff";
bar.life = 6;
bar.exp = 100;

//  You can create any number of different objects, and each object will have its own copy of
//   its members
AlienStats  baz;  // <- baz.name is a different string from bar.name, etc.

// What you CAN'T do, is try to access name/life/exp without an object
//   An object must be left of the dot (.) operator to access individual members:
name = "Booger";  // <- this doesn't make sense... WHOSE name?  You have to give it an object.  This is an ERROR
bar.name = "Booger";  // <- bar's name!  This is OK 



A class is the exact same idea, only they're usually coupled with member functions which act on the current object (also known as "this" object, as "this" is a special keyword in C++ to refer to the current object).

Example:

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
class Alien
{
private:
    // member variables:  things that EACH alien has:
    std::string name;
    int life;           // <- how much life this alien has
    bool alive;         // <- whether or not this alien is alive
    
public:
    // member functions:  functions which act on an individual object / individual Alien
    void hurt()     // <-  function to hurt this alien
    {
        life -= 1;          // take some of 'this' alien's life away (note:  keyword 'this' explained more below)
        if(life <= 0)
            alive = false;  // <- no more life = dead
    }
};

// you could then use this class like so:
Alien biff;

biff.hurt();  // <- hurt the biff Alien (and if his life drops below zero, kill him)

// but again... you always need an object:
hurt();   // <- doesn't make sense.  Who are you hurting?  ERROR
Alien::hurt();  // <- also doesn't make sense. Which Alien are you hurting?  ERROR 


But wait.... up above when I was talking about structs I told you that you couldn't access members unless they had an object with a dot operator.
1
2
bar.name = "kdlsfjd";  // OK
name = "whatever";  // ERROR 


BUT, in my example for the hurt() function, I use 'life' and 'alive' without using an object! Isn't that an error?

No! Because we do have an object, it's just hidden. Since hurt() is a member function (that is, it is inside the class), there is a hidden 'this' object. We can access members without specifying an object, and the compiler will assume we are using 'this' object's members:

1
2
3
4
5
Alien ab;  // a couple of Alien objects
Alien cd;

ab.hurt();  // <- hurt the ab object.  This will call hurt() with this=ab.  So it will subtract from ab.life and set ab.alive
cd.hurt();  // <- This will call hurt() with this=cd, so it will subtract from cd.life, etc. 




Hopefully that explains it a bit better. Feel free to ask if you have more questions or if anything is unclear.
Last edited on
Topic archived. No new replies allowed.