This is quite literally driving me insane. I'm new to C++ so bear with me on this one.

Basically, I've got two files. One header file (Package.h) and the C++ file (Package.cpp)

I've got a class called Package and function prototype in the header file as follows

private:
string senderName;
public:
void setSenderName( const string & );

So when I try to define the function in the main Package.cpp file like this

void Package::setSenderName( const string &n )
{
	senderName = n;
}

I get the following error: "[Error] C:\Users\Flynn\Documents\Cpp\Package.cpp:26: `Package::setSenderName(const string &)' has already been declared in `Package'"

Based on the textbook and examples I've followed this really shouldn't be happening and I don't know where I've gone wrong. Both files are in the same directory and I am using C-Free if that helps. Anyone care to help out?

Thanks in advance

FlynnLags
Deleted Member

Either you have a multiple inclusion, or you duplicated your declaration statement somewhere, or your function call isn't formatted correctly. You'll have to post more of your code to tell though, there's just not enough here.

Edited 6 Years Ago by Fbody: n/a

Thanks for replying. Here's a bit more of the codes.

Package.h header file

#ifndef PACKAGE_H
#define PACKAGE_H

#include <string>
using std::string;

class Package
{
public:
// constructor initiliazes data members
	Package( const string &, const string &, const string &,
             const string &, int, const string &, const string &, const string &, const string &, int, double, double );

	void setSenderName( const string & ); // set sender's name
	string getSenderName() const; // return sender's name
	void setSenderAddress( const string & ); // set sender's address
	string getSenderAddress() const; // return sender's address
	void setSenderCity( const string & ); // set sender's city
	string getSenderCity() const; // return sender's city
	void setSenderState( const string & ); // set sender's state
	string getSenderState() const; // return sender's state
	void setSenderZIP( int ); // set sender's ZIP code
	int getSenderZIP() const; // return sender's ZIP code
	void setRecipientName( const string & ); // set recipient's name
	string getRecipientName() const; // return recipient's name
	void setRecipientAddress( const string & ); // set recipient's address
	string getRecipientAddress() const; // return recipient's address
	void setRecipientCity( const string & ); // set recipient's city
	string getRecipientCity() const; // return recipient's city
	void setRecipientState( const string & ); // set recipient's state
	string getRecipientState() const; // return recipient's state
	void setRecipientZIP( int ); // set recipient's ZIP code
	int getRecipientZIP() const; // return recipient's ZIP code
	void setWeight( double ); // validate and store weight
	double getWeight() const; // return weight of package
	void setCostPerOunce( double ); // validate and store cost per ounce
	double getCostPerOunce() const; // return cost per ounce
	double calculateCost() const; // calculate shipping cost for package
private:
// data members to store sender and recipient's address information
	string senderName;
	string senderAddress;
	string senderCity;
	string senderState;
	int senderZIP;
	string recipientName;
	string recipientAddress;
	string recipientCity;
	string recipientState;
	int recipientZIP;
	double weight; // weight of the package
	double costPerOunce; // cost per ounce to ship the package
; // end class Package

#endif

Package.cpp still work in progress so not all functions have been defined but given the errors I won't define all functions until I know whats wrong

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

Package::Package(const string &sname, const string &saddress, const string &scity, const string &sstate, const string &szipcode,
				const string &rname, const string &raddress, const string &rcity, const string &rstate, const string &rzipcode, double wt, double shipCost)
{
        senderName = sname;
        senderAddress = saddress;
        senderCity = scity;
        senderState = sstate;
        senderZIP= szipcode;
        recipientName = rname;
        recipientAddress = raddress;
        recipientCity = rcity;
        recipientState = rstate;
        recipientZIP = rzipcode;
        setWeight(wt);
        setCostPerOunce(shipCost);
}

void Package::setSenderName( const string &n )
{
	senderName = n;
}

Lines 52 and 53 of Package.h. You missed the closing brace for the class definition. You need both a closing brace and a semi-colon on Line 53.

Appreciate the help Fbody, that was all it took =) Can't believe it was that simple. I could've sworn I fixed that earlier =/

So for future reference, possible causes of my problem would be:

- Multiple inclusions
- Duplicate declarations
- Syntax errors

The multiple inclusion is always a potential issue. However, you have header guards in place so that should eliminate that as a possibility this time.

The other 2 possibilities would be more likely.

This question has already been answered. Start a new discussion instead.