Class Definition Unrecognized by Compiler

So I'm working on a project and I'm trying to make a simple class which. for some reason, isn't being recognized by the compiler. Any time I declare an object of it in another file I get an "undeclared identifier" error.

My class is as follows:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef PLANET_H
#define PLANET_H

#include <iostream>
#include "ArrayList.h" //I haven't received any compiler errors regarding this
#include "Star.h"      //or this for that matter.

class Planet
{
private:
    char name; // Primary name of the planet
    Star* sun; // The star that the planet orbits

    int neighbors; // Number of other stars in the system
    float msini; // Planet's minimum mass
    float a; // Semi-major axis
    float per; // Orbital period
    float ecc; // Eccentricity of orbit
    float om; // "Argument of Periastron"
    float t0; // Time-length of planet's periastron passage
    float k; // Semiamplitude of the planet's Doppler Variation

public:
    /* mutators */
    void setName(char n)
    {
        name = n;
    }
    void setSun(Star s)
    {
        sun = &s;
    }
    void setNeighbors(int n)
    {
        neighbors = n;
    }
    void removeNeighbor()
    {
        --neighbors;
    }
    void setMSINI(float m)
    {
        msini = m;
    }
    void setAxis(float ax)
    {
        a = ax;
    }
    void setPer(float p)
    {
        per = p;
    }
    void setEcc(float e)
    {
        ecc = e;
    }
    void setOm(float o)
    {
        om = o;
    }
    void setT0(float t)
    {
        t0 = t;
    }
    void setK(float ki)
    {
        k = ki;
    }

    /* accessors */
    char getName() const
    {
        return name;
    }
    Star getSun() const
    {
        return *sun;
    }
    int numNeighs() const
    {
        return neighbors;
    }
    float getMSINI() const
    {
        return msini;
    }
    float getAxis() const
    {
        return a;
    }
    float getPer() const
    {
        return per;
    }
    float getEcc() const
    {
        return ecc;
    }
    float getOm() const
    {
        return om;
    }
    float getT0() const
    {
        return t0;
    }
    float getK() const
    {
        return k;
    }

    /* overloaded operators */
    Planet& operator =(const Planet& c)
    {
        return Planet(c);
    }

    /* constructors */
    Planet()
    {
        name = '\0';
        sun = nullptr;
        neighbors = 0;
        msini = 0.0;
        a = 0.0;
        per = 0.0;
        ecc = 0.0;
        om = 0.0;
        t0 = 0.0;
        k = 0.0;
    }
    Planet(const Planet& c)
    {
        name = c.getName();
        sun = &c.getSun();
        neighbors = c.numNeighs();
        msini = c.getMSINI();
        a = c.getAxis();
        per = c.getPer();
        ecc = c.getEcc();
        om = c.getOm();
        t0 = c.getT0();
        k = c.getK();
    }
	Planet(istream& in)
	{

	}
    ~Planet()
    {}
};

std::ostream& operator <<(ostream& o, const Planet& p)
{
    o << p.getSun().getName() << " " << p.getName() << "," << p.numNeighs() << "," << p.getMSINI() << "," << p.getAxis() << "," << p.getPer() << "," << p.getEcc() << "," << p.getOm() << "," << p.getT0() << "," << p.getK() << "," << p.getHome().isBinary();
}

#endif // EXOPLANET_H 
Does your Star header also include this header? If so, you have a circular dependency - this is how you fix that:
http://www.cplusplus.com/articles/Gw6AC542/
I was thinking that might be the case. I put the Star class in this header, but now it's making a similar complaint about the Star* in this class.
Did you read that article? It explains how to fix that.
I tried a fix explained in the article, putting a templated/typedefed forward declaration of class "Star" before my "Planet" class declaration. I put my star class in the same file as my planet class, and finally, I put the definition of the planet class in a separate file. All this seems to not have worked. Now I'm getting a "redefinition; different basic types" error at the top of my build log.
Last edited on
The terms "template" and "typedef" should not be anywhere near the term "forward declaration" - what exactly is your current code?
Never mind I've found a way around it. My code works well enough now.
You should modify it to say "thank you"
Last edited on
kkhalaf wrote:
You should modify it to say "thank you"
What for?
DO NOT edit this for the "thank you".
You WILL get hate. I hate it myself.

Just mark it as solved, there is a button for that if you really feel like - or just leave it as it is.
If you feel extra nice, point out how you did it, so that this post can become useful for future visitors.
Topic archived. No new replies allowed.