Hi,

Being someone who is now working with C++ but initially started with Java, I am experiencing some problems seperating my classes from the main file.
So I want to seperate the following simple class into it's own standalone C++ file so I can use it with multiple programs:

#include <iostream>
#include <string>

using namespace std;

class signal
{
	private:
		int decAddress;
		string binAddress;
		string data;
	public:
		signal(string *binAddr, string *da, int decAddr)
		{
			binAddress = *binAddr;
			data = *da;
			decAddress = decAddr;
		}

		string getBinAddress()
		{
			return binAddress;
		}
		
		void setAddress(string binAddr)
		{
			binAddress = binAddr;
		}
		
		int getDecAddress()
		{
			return decAddress;
		}
		
		void setDecAddress(int decAddr)
		{
			decAddress = decAddr;
		}
		string getData()
		{
			return data;
		}
		
		void setData(string da)
		{
			data = da;
		}
};

So I also created a header file to go along with it:

#include <string>
#include <iostream>

using namespace std;

class signal
{
	private:
		int decAddress;
		string binAddress;
		string data;
	public:
		signal(string *binAddr, string *da, int decAddr);
		string getBinAddress();
		void setAddress(string binAddr);
		int getDecAddress();
		void setDecAddress(int decAddr);
		string getData();
		void setData(string da);
};

In my main class, I then add the line:

#include <signal.h>

I'm using GCC to compile and run the program, so I first compile the class into an object file and recieve no errors:

g++ -c signal.cpp

Then I try to run my program linking the signal class:

g++ main.cpp -o myprogram signal.o

and I'm getting a whole bunch of errors like this:

myprogram.cpp:220: error: ISO C++ forbids declaration of `signal' with no type

Where it basically seems like my class file is still not being recognized. The problem does not have to do with the naming of my file either. Does anyone know what the problem is?

Thanks,

bleh

Can you post main.cpp as well?

First, try using

#include "signal.h"

instead of

#include <signal.h>

In your signal.cpp file, don't redeclare the class, just define the functions.

#include <iostream>
#include <string>
#include "signal.h"

using namespace std;

signal::signal(string *binAddr, string *da, int decAddr)
{
   // do stuff
}

string signal::getBinAddress()
{
	return binAddress;
}

void signal::setAddress(string binAddr)
{
	binAddress = binAddr;
}

int signal::getDecAddress()
{
	return decAddress;
}

void signal::setDecAddress(int decAddr)
{
	decAddress = decAddr;
}
string signal::getData()
{
	return data;
}

void signal::setData(string da)
{
	data = da;
}

You may move originally posted class signal declaration to signal.h file as is - no need to create cpp file for the class with only inline member functions - all member functions defined in the class declarations. Now include "signal.h" - that's all.

But there are suspicious parameters in the signal constructor:

signal::signal(string *binAddr, string *da, int decAddr)

Why string* binAddr, string* da... pointers to string? May be, you want

signal::signal(const std::string& binAddr, const std::string& da, int decAddr):
binAddress(binAddr), data(da), decAddress(decAddr)
{}

?

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.