"x" does not name a type

I'm following a tutorial to build a particle explosion project. I'm working in steps and creating a new project for each step. In order to get practice typing out the code, for each new project, I retype the code from the previous project before adding the new step.

When I type out the code in the new project, I get error messages and the project will not build and run. It's exactly the same code as the previous project that builds and runs just fine! I've double checked and triple checked to make sure that I didn't make a mistake in typing.

The project has 3 classes: Screen, Particles and Swarm. In this step, I've retyped the code that makes the particles move. *Again, the previous project is running perfectly:

In Swarm.h :

1
2
3
4
5
6
7
8
9
10
class Swarm {
public:
     const static int NPARTICLES = 3000;
private:
     Particles * m_pParticles;
public:
     Swarm ( );
     void update ( );
     const Particles * const getParticles ( ) { return m_pParticles; };  
};


The error is "Particles" does not name a type at lines 5 and 9.

In the main.cpp file, I get the related error "class Swarm" has no member named getParticles.
I have tried cleaning the build, changing the Build Configuration from "Use Active" to "Select Automatically" (this step fixed the problem in the previous step). I tried shutting down and restarting.
I don't know what else to try.
I'm using Eclipse IDE 2020-06 (4.16.0) with MinGW-64 and SDL2 on a Windows 10 laptop.
Last edited on
Hello ConCoder58,

This may help.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Do to lack of code I can only guess what you did is:
1
2
3
4
5
6
7
8
9
10
11
class Swarm
{
// Code here.
private:
      Particles * m_pParticles;
}

class Particles
{
// code here.
}

When the compiler compiles the code it works with 1 line at a time, so it compiles "Swarm" first, but has not seen "Particles" yet thus the error.

Sometmes order does make a difference.

Try putting "Particles" first and see what happens.

Andy
Hello Andy,

I did highlight the code part and clicked the code formatting button before I posted. I edited it and did it again. I had to do my own indentation.

As I mentioned in the post, this exact code runs perfectly in the original project. The only difference between the two is that I named the Particle class "Particles".

There is no reason that I can understand why this won't work. Everything is exactly the same as in the original code. I was hoping that someone else has had a similar issue and can help me figure out.
can you post 100% of swarm.h ? or up to class swarm { statement at least ... includes, everything?
The only difference between the two is that I named the Particle class "Particles".

Did you do the name changes manually? I'd bet you didn't change ALL of the code that uses the class.
Hello ConCoder58,

Sorry. In the first post I seem to remember that the line numbers being on the inside of the box. Anyhow the second link is worth keeping.

I hope you did not get the wrong idea because that quoted box is just a copy and paste.

You should post the whole code that is giving you the current problem.

To add to what Furry Guy said it is hard to proof read what you write. You do not always see the mistakes that are staring you right in the face. Having someone else go over the code can find something that you do not see.

I agree that making changes manually you always miss something. Your IDE should have a find and replace that is worth using as often as possible.

Andy
1
2
3
4
5
class Particles; // add this

class Swarm {
    // ...
};
Last edited on
OMG!! Furry Guy was right!! I missed two lines of code in the Swarm.cpp file!! I don't know how I could be so blind!!

Thank you all for actually trying to help instead of just berating me for "posting off topic" like some other forums.
Thank you all again. I'm a complete newbie, but I am learning!

Andy, the numbers were on the inside of the box because I didn't understand that the code formatting would put the numbers there for me.

I edited it after you explained about using Preview.

You guys are awesome!! Thanks again.

I missed two lines of code in the Swarm.cpp file!! I don't know how I could be so blind!!

I suspected that was the problem because I was bitten by the very same problem more than once.

Then I started using the "global find and replace" editor features.

With Visual Studio 2017/2019 there is an option to globally rename a variable/class/etc. I use that a LOT! It is part of the "refactoring" options.

One thing to note about the preview feature of the editor here. Preview won't work in the initial post before it is saved. Editing the initial post, or adding follow-up posts preview does work.
Hey Folks,

I'm back. I'm still getting the error and now I have a new one. Here is the Swarm.h for Project B (the one that doesn't work)

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
#ifndef SWARM_H_
#define SWARM_H_

#include <iostream>
#include "Particles.h"
#include "Screen.h"

namespace screenanimation {

class Swarm {

public:
	const static int NPARTICLES = 4000;

private:
	Particles * m_pParticles;

public:
	Swarm ();
	virtual ~Swarm ();
	void update ();

	const Particles * const getParticles () { return m_pParticles; };
};

} /* namespace screenanimation */

#endif /* SWARM_H_ */ 


The error 'Particles does not name a type' show at lines 16 and 23.

Here is the code for Swarm.h for Project A (the one that runs)

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
#ifndef SWARM_H_
#define SWARM_H_

#include <iostream>
#include "Particle.h"

namespace screenanimation {

class Swarm {
public:
	const static int NPARTICLES = 4000;

private:
	Particle * m_pParticles;

public:
	Swarm();
	virtual ~Swarm();
	void update ();

	const Particle * const getParticles () { return m_pParticles; };
};

} /* namespace screenanimation */

#endif /* SWARM_H_ */ 


The new error is in Swarm.cpp (Project B)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Swarm.h"

namespace screenanimation {

Swarm::Swarm () {
	m_pParticles = new Particles [NPARTICLES];
}

Swarm::~Swarm () {
	delete [] m_pParticles;
}

void Swarm::update () {
	for (unsigned i = 0; i < Swarm::NPARTICLES; i++) {
		 m_pParticles [i].update ();
	}

}
} /* namespace screenanimation */


The error is at line 15 "method 'update' could not be resolved".

Swarm.cpp (Project A)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Swarm.h"

namespace screenanimation {

Swarm::Swarm() {
	m_pParticles = new Particle [NPARTICLES];
}

Swarm::~Swarm() {
	delete [] m_pParticles;
}

void Swarm::update() {
	for (unsigned i = 0; i < Swarm::NPARTICLES; i++) {
		m_pParticles [i].update ();
	}

}

} /* namespace screenanimation */


I am also still getting the error about Swarm having no member "getParticles"
If the error is that Particles does not name a type, show us what's in Particles.h.

Also, if you aren't using polymorphism, there isn't a point to making the destructor be virtual. This is not related to your actual issue, though.
Last edited on
Guess what?? I decided to delete the project and start over and it works great!! So, whatever the problem was, it's gone.
read this http://www.cplusplus.com/forum/articles/10627/
your problem was circular includes, don't include unless necessary
Topic archived. No new replies allowed.