I'm trying to work out what should be a simple composition example. For some reason I can't compile this project though. I get three errors when I try to compile. Errors listed below. Where am I going wrong?

1>c:\documents and settings\user\my documents\visual studio 2008\projects\name\name\ssn.h(5) : error C2011: 'SSN' : 'class' type redefinition

1>c:\documents and settings\user\my documents\visual studio 2008\projects\name\name\name.cpp(9) : error C2027: use of undefined type 'SSN'

1>c:\documents and settings\user\my documents\visual studio 2008\projects\name\name\name.cpp(9) : fatal error C1903: unable to recover from previous error(s); stopping compilation

//Name.h
#include <string>
using std::string;

#include "SSN.h"

class Name
{
public:
	Name();
	Name(const string &, const string &);
	Name(const string &, const string &, const SSN &);

	string getFIRST() const;
	string getLAST() const;

	void setFIRST(const string &);
	void setLAST(const string &);
private:
	string FIRST;
	string LAST;
	const SSN SSAN;
};
//Name.cpp
#include <string>
using std::string;

#include "Name.h"
#include "SSN.h"

Name::Name()
: SSAN("000-00-0000")
{
	setFIRST("Joe");
	setLAST("Cool");
}

Name::Name(const string &f, const string &l)
:	SSAN("000-00-0000")
{
	setFIRST(f);
	setLAST(l);
}

Name::Name(const string &f, const string &l, const SSN &s)
:	SSAN(s)
{
	setFIRST(f);
	setLAST(l);
}

string Name::getFIRST() const
{
	return FIRST;
}

string Name::getLAST() const
{
	return LAST;
}

void Name::setFIRST(const string &f)
{
	FIRST = f;
}

void Name::setLAST(const string &l)
{
	LAST = l;
}
//SSN.h
#include <string>
using std::string;

class SSN
{
public:
	SSN();
	SSN(const string &);

	void setSSN(const string &);
	string getSSN() const;
private:
	string SOCIAL;
};
//SSN.cpp
#include <string>
using std::string;
#include "SSN.h"

SSN::SSN()
{
	setSSN("000-00-0000");
}

SSN::SSN(const string &s)
{
	setSSN(s);
}

void SSN::setSSN(const string &s)
{
	SOCIAL = s;
}

string SSN::getSSN() const
{
	return SOCIAL;
}
//driver.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

#include "Name.h"

int main()
{
	Name joe;
	cout << joe.getFIRST() << " " << joe.getLAST() << endl;

	string f, l;

	cout << "ENTER FIRST NAME:";
	cin >> f;
	cout << "ENTER LAST NAME:";
	cin >> l;

	Name chris(f, l);

	cout << chris.getFIRST() << " " << chris.getLAST() << endl;

	return 0;
}

Header files are usually included more than once, but you can only have one class definition. There are ways to make sure that a header isn't included more than once even if the programmer tries, and Edward's recommendation is header guards:

#ifndef NAME_H
#define NAME_H

//Name.h
#include <string>
using std::string;

#include "SSN.h"

class Name
{
public:
	Name();
	Name(const string &, const string &);
	Name(const string &, const string &, const SSN &);

	string getFIRST() const;
	string getLAST() const;

	void setFIRST(const string &);
	void setLAST(const string &);
private:
	string FIRST;
	string LAST;
	const SSN SSAN;
};

#endif
#ifndef SSN_H
#define SSN_H

//SSN.h
#include <string>
using std::string;

class SSN
{
public:
	SSN();
	SSN(const string &);

	void setSSN(const string &);
	string getSSN() const;
private:
	string SOCIAL;
};

#endif

The preprocessor checks for a defined name--it can be anything you want as long as it's unique--and if the name doesn't exist, it defines the name and includes the header. Otherwise it just doesn't include the header. See if that fixes your errors.

Wow, I forgot about that. Thank you!

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.