This is my first post here, but I have been using the forum for many months and up untill now has been able to answer all my problems without me actually asking anything. So thanks in advance for all the great work you guys do, you have been fantastic in the past.

So, quick explanation. I have a class, called particle, which containts the function called Assign. Assign works perfectly where it is, but I want to move it. So after a bit of googling it seems the best way to do this is to use "::".

So in a new header simply write void particle::Assign(.....)

However I am getting error C2653: 'particle' : is not a class or namespace name. I do not know why, googling the error seems to suggest that the class "particle" does not exist. It does, it is right there.

I suspect I am doing something daft, and is easy and obvious to fix, but a good hour of searching and reading various past threads has not helped.

So, here is my code:

First the relevant bits of particle class, called particle2.h:

#include <math.h>
#include <vector>
#include <iostream>
#include <stdio.h>
#include "threevector.h"
#include "constants.h"
#include "initial_line.h"
using namespace std;


class particle
{
	double x;
	double y;
	double z;
	double q;
	double m;
	double ux;
	double uy;
	double uz;
	double hux;
	double huy;
	double huz;


public:

	particle()
	{
		x=0;
		y=0;
		z=0;
		q=0;
		m=0;
		ux=0;
		uy=0;
		uz=0;
		hux=0;
		huy=0;
		huz=0;
	}

	particle(double x, double y, double z, double q, double m, double ux, double uy, double uz, double hux, double huy, double huz)
	{
		this->x = x;
		this->y = y;
		this->z = z;
		this->q = q;		//charge
		this->m = m;		//mass
		this->ux = ux;		//initial speed
		this->uy = uy;		//initial speed
		this->uz = uz;		//initial speed
		this->hux = hux;		//previous half speed
		this->huy = huy;		//previous half speed
		this->huz = huz;		//previous half speed

	}

	
void Assign(double, double, double, double, double, double, double, double, double, double, double);
	

					double getx() const
					{
						return x;
					}


					double gety() const
					{
						return y;
					}


					double getz() const
					{
						return z;
					}


					double getm()  
					{
						return m;
					}


					double getq()  
					{
						return q;
					}

					double getux()  
					{
						return ux;
					}


					double getuy()  
					{
						return uy;
					}


					double getuz()  
					{
						return uz;
					}

					double gethux()  
					{
						return hux;
					}


					double gethuy()  
					{
						return huy;
					}


					double gethuz()  
					{
						return huz;
					}

					void setx(double x)
					{
						this->x = x;
					}

					void sety(double y)
					{
						this->y = y;
					}

					void setz(double z)
					{
						 this->z = z;
					}

					void setux(double ux)
					{
						 this->ux = ux;
					}

					void setuy(double uy)
					{
						 this->uy = uy;
					}

					void setuz(double uz)
					{
						 this->uz = uz;
					}

					void sethux(double hux)
					{
						 this->hux = hux;
					}

					void sethuy(double huy)
					{
						 this->huy = huy;
					}

					void sethuz(double huz)
					{
						 this->huz = huz;
					}

					void setq(double q)
					{
						 this->q = q;
					}

					void setm(double m)
					{
						 this->m = m;
					}

And here is the new header I have written, called initial_line.h:

#include "particle2.h"
using namespace std;

void particle::Assign(double x, double y, double z, double q, double m, double ux, double uy, double uz, double hux, double huy, double huz)
{
		x = x / b0;
		y = y / b0;
		z = z / b0;
		q = q / e;
		m = m / me;
		ux = ux / b0;
		uy = uy / b0;
		uz = uz / b0;
		hux = hux / b0;
		huy = huy / b0;
		huz = huz / b0;

		setx(x);
		sety(y);
		setz(z);
		setq(q);
		setm(m);
		setux(ux);
		setuy(uy);
		setuz(uz);
		sethux(hux);
		sethuy(huy);
		sethuz(huz);

		
	}

particle 2.h is the file that the particle class is in.

So, the erros I get are

h:\treecode\treecode\initial_line.h(4) : error C2653: 'particle' : is not a class or namespace name

and then a load of errors for the set funtions eg.: 1>h:\treecode\treecode\initial_line.h(18) : error C3861: 'setx': identifier not found


I have tried various combinations of including and not including header files, and changing the names of the class and the funtion.

So, I am at a loss. It seems a simple enough concept that is just no working. I am using C++ within VS2008 pro, version 9. (Previous posts blamed errors in old compliers for problems)

So, I would be very grateful of any help, and to anyone pointing out what I have done wrong.

The funtion worked perfectly fine inside particle, and I assume all the errors involving the set funtions will go away onve it is properly linked with the particle class.

Cheers

Josh

Personally, I'd leave only the declarations of the member variables and functions for your class in the header file and then put the definitions of the functions into a .cpp file which has a filename that corresponds with the header. (e.g. particle2.h and particle2.cpp)
Your get and set functions will probably be ok defined in the header as they are pretty small, but all of the other functions should be declared in the header and defined in the .cpp file.
So your header would look something like this:

// This is particle2.h
// Add whatever includes you need here
// At the moment none are needed, but I guess you may have other functions in here which use vectors etc.!

// Note: If you do have any functions which take other datatypes/classes as parameters
// you might not need includes here, you can forward declare classes like this:
// class std::vector; // forward declares the std::vector class
// once you've forward declared something, you should #include its header in your .cpp file
// Also Note: Sometimes forward declaring doesn't work, so you might have to include the header here instead of forward declaring!
// Offhand I can't remember the golden rule with regard to forward declaring things!

// inclusion guards - ensures this header is only included once.
#ifndef PARTICLE2_DEFINED
#define PARTICLE2_DEFINED

class particle
{
	// private data
	double x;
	double y;
	double z;
	double q;
	double m;
	double ux;
	double uy;
	double uz;
	double hux;
	double huy;
	double huz;


public:
	// default constructor - initialise all members to 0.
	particle():x(0),y(0),z(0),q(0),m(0),ux(0),uy(0),uz(0),hux(0),huy(0),huz(0){};
	// overloaded constructor
	particle(double, double, double, double, double, double, double, double, double, double, double);

	// Other public functions
	void Assign(double, double, double, double, double, double, double, double, double, double, double);
	

	// Getter functions
	double getx() const{return x;}
	double gety() const{return y;}
	double getz() const{return z;}
	double getm() const{return m;}
	double getq() const{return q;}  
	// ETC ETC..
	// rest of the getters go here

	// Setter functions
	void setx(double x){this->x = x;}
	void sety(double y){this->y=y;}
	// ETC ETC..
	// rest of your setters go here...

	// Any other relevant functions can go here.
	// Note: Only put the declarations here and put the implementations into the .cpp file
};

#endif // PARTICLE2_DEFINED

And your .cpp file will look something like this:

// particle2.cpp

#include "path/to/particle2.h"
#include "constants.h" // I assume that's where b0, e and me are defined!
// obviously add any additional includes you need here
// and any includes for anything that was forward declared in the header

// overloaded constructor
particle::particle(double x, double y, double z, double q, double m, double ux, double uy, double uz, double hux, double huy, double huz)
{
	this->x = x;
	this->y = y;
	this->z = z;
	this->q = q;		//charge
	this->m = m;		//mass
	this->ux = ux;		//initial speed
	this->uy = uy;		//initial speed
	this->uz = uz;		//initial speed
	this->hux = hux;	//previous half speed
	this->huy = huy;	//previous half speed
	this->huz = huz;	//previous half speed
}

// Assign function
void particle::Assign(double x, double y, double z, double q, double m, double ux, double uy, double uz, double hux, double huy, double huz)
{
	x = x / b0;
	y = y / b0;
	z = z / b0;
	q = q / e;
	m = m / me;
	ux = ux / b0;
	uy = uy / b0;
	uz = uz / b0;
	hux = hux / b0;
	huy = huy / b0;
	huz = huz / b0;

	setx(x);
	sety(y);
	setz(z);
	setq(q);
	setm(m);
	setux(ux);
	setuy(uy);
	setuz(uz);
	sethux(hux);
	sethuy(huy);
	sethuz(huz);
}

// any additional class function definitions can go here

If you stick to the header and matching .cpp file convention as outlined above you should be ok. If you'd put your function definition into a cpp file called particle2.cpp and then included particle2.h, your program would have compiled ok.

The problem you were having was most likely down to something like file-scope or a namespace issue.
Because you put your function definition into another header file (initial_line.h), the compiler was getting confused (or perhaps its a deliberate feature of the compiler!). The particle class is declared in particle2.h. AFAIK, if the compiler can't find a definition for a function in the header it will look for it in a .cpp file with the same name as the header (If there is one!). But because your function definition was in initial_line.h, it didn't recognise the particle::assign method as being a part of the particle class declared in particle2.h it thought you were trying to declare something in a non existent namespace (particle:: ) inside initial_line.h's namespace.

I could be wrong on that, and I'm not entirely sure I'm explaining things particularly correctly or clearly. But I'm sure that Narue or one of the other mods/ubergurus here will correct me!

Anyway, I hope that is of some help to you!
Cheers for now,
Jas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.