I'm trying to create a contributor class with one enumerated type. I took the member functions from the header and put them in a cpp, but I get an error for the enumerated type when I try to compile. Is it because I haven't written a code body for that member function yet? Files below, header first...oh, I'd also like to ask if everything looks okay so far.

#pragma once
#include <string>
#include <iostream>
using namespace std;

enum gender {male,female,none};

class Contributor
{
	public:
		Contributor();
		Contributor (string Name, double Donation =0.0, gender sex=none, int IDKey=0);
		Contributor (const Contributor& InContributor);
		~Contributor();
		friend ostream& operator << (ostream& out, Contributor& InContributor);
		friend istream& operator >> (istream& in, Contributor& InContributor);
		Contributor& operator = (const Contributor& rtside);
		bool operator <  (const Contributor& rtside);
		bool operator >  (const Contributor& rtside);
		bool operator == (const Contributor& rtside);
		bool operator != (const Contributor& rtside);

	private:
		string Name;
		double Donation;
		int IDKey;
		enum gender;
};

Here's the beginning cpp, no function defs yet

#include "Contributor class.h"


Contributor::Contributor()
{
}

Contributor::Contributor (string Name, double Donation, gender sex, int IDKey)
{
}
		
Contributor::Contributor (const Contributor& InContributor)
{
}

Contributor::~Contributor()
{
}

ostream& operator << (ostream& out, Contributor& InContributor)
{
}

istream& operator >> (istream& in, Contributor& InContributor)
{
}

Contributor& Contributor::operator = (const Contributor& rtside)
{
}
		
bool Contributor::operator <  (const Contributor& rtside)
{
}

bool Contributor::operator >  (const Contributor& rtside)
{
}

bool Contributor::operator == (const Contributor& rtside)
{
}

bool Contributor::operator != (const Contributor& rtside)
{
}

Dani AI

Generated

Brief recap and what actually fixed the original C2511: was right about a scope/type mismatch (global enum vs a nested/declared type), and ’s suggestion to make a real member (for example gender Sex;) solved that specific compiler complaint. The root cause was that the parameter type used in the header must match exactly the type used in the .cpp definitions — if the enum is nested inside the class its full name is Contributor::gender; if it’s global it’s just gender. Declaring enum gender; inside the class does not create a member variable.

Why the new errors appear: several functions in the .cpp have non-void return types but empty bodies, so the compiler will complain that a value must be returned. Practical fixes (in prose): make the streaming operator return the stream after writing/reading the members; make the assignment operator copy members and return *this; make comparisons const and return a boolean based on whatever key(s) (ID, name, donation) are appropriate.

Other cleanups and best practices to avoid future trouble:

  • Match signatures exactly between header and .cpp. If the enum is a nested type, use the qualified name in definitions.
  • Prefer const std::string& for string parameters to avoid needless copies.
  • Initialize members with constructor initializer lists rather than assigning in the body.
  • Make operator<< take the object by const&, operator>> by non-const reference, and have both return the stream.
  • Avoid using namespace std; in headers.
  • If the class has no manual resource management, the destructor can be defaulted/omitted.
  • Turn on high compiler warning levels so “missing return” or signature mismatches show up clearly.

Checklist: confirm header prototypes, confirm .cpp definitions use identical types (watch enum qualification), implement return statements, rebuild with warnings enabled. ’s original change to a real member removed the overload mismatch; the remaining compiler messages are straightforward missing-return/signature issues that the above steps will resolve.

Recommended Answers

All 6 Replies

Err, what exactly is the error you get? From what line?

error C2511: 'Contributor::Contributor(std::string,double,Contributor::gender,int)' : overloaded member function not found in 'Contributor'
see declaration of 'Contributor'

error C2511: 'Contributor::Contributor(std::string,double,Contributor::gender,int)' : overloaded member function not found in 'Contributor'
see declaration of 'Contributor'

Can't say I'm 100% sure - but after looking at it a bit I'm pretty sure:

You have two enumerations. The first is gender, which is declared and defined in global scope. The second is Contributor::gender, which is declared as a private member of the Contributor class. When you declare the constructor we're dealing with in the .h file, one of it's types is gender, and since we're in the scope of Contributor, it is assumed that gender refers to Contributor::gender. However, when you define the constructor in the .cpp file, you say gender once again, only this time, you're referring to the global gender enumeration (since we're in global scope), and so the compiler complains that you're defining an overloaded method with different parameter types than any of the ones in the class declaration.

I'm assuming you only wanted one enumeration. Take the line that first declares gender (outside of the class), and move it to the top of the .cpp file, and change it from enum gender ... to enum Contributor::gender...

thanks for the attempt cg, but that wasn't it. I only compounded my problem with that

error C2511: 'Contributor::Contributor(std::string,double,Contributor::gender,int)' : overloaded member function not found in 'Contributor'
see declaration of 'Contributor'

In the class declaration, you probably meant to have a member variable like gender Sex; instead of enum gender;

that seems to have done it, now I have the errors I expected to have for empty functions that should be returning values

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.