Okay, I cannot figure this out. I still get the same error:

Error 1 error C2039: 'function' : is not a member of 'D'

Here's what I have:

Class A.h:

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

class D;

class A
{
    public:
		void function(string s);
		friend class D;
        
    private:
        string name;
        string email;
        
    };

Class D.h:

#pragma once
#include <vector>
#include "A.h"

class L;
class ListOfD;

class D
{
	public:
		friend class L;
		friend class ListOfD;
        
    private:
              //not important right now
};

Class L.cpp(this is where the error is):

#include "L.h"
string L::somethingnotimportant(string & command)
{
    string result;
    if(command.substr(0,4)=="some")
	{
		string parameter=command.substr(5,9999);
		D p;
    --------->   p.function(parameter);
		lop.lastID++;
		p.patronIdNumber = lop.lastID;
		lop.add(p);		
	}
	else if(command == "PRINTP")
	{
		//result = printp();
	}
    else if(command == "QUIT")
    {
        result = "Goodbye!";
    }
    else
    {
        result = "UNKNOWN COMMAND";
    }
    
    return result;
}

Okay, I think thats enough code to see the problem. I left out some information, so no one steals this code. Anyways, the function from class A is suppose to be inherited from class D, as well as the two string variables, right? Because nothing from class A can be used unless I create a new object A.

Recommended Answers

All 7 Replies

Your D class does not inherit from class A. You should write

class D : A
{
// your code here
};

to inherit from class A.

Thanks a lot for your help. I thought that if you included the header file in the other class, then it would inherit it (at least, thats what the professor said). Thanks again!

Take a look at this, it should explain the general concept :)

ughhh...okay, class D is now able to access class A's function (yay!), but now it doesn't seem to be able to access the private members. Why? I thought when you used friend operator, it allows access to private members..?

ughhh...okay, class D is now able to access class A's function (yay!), but now it doesn't seem to be able to access the private members. Why? I thought when you used friend operator, it allows access to private members..?

Not sure, change them to protected should work though.

Not sure, change them to protected should work though.

yea, you're right, it does work. Unfortunately, the private members need to be private. So what should I do???

Protected means private but inherited like private (hence accessible) by derived classes. If you declare them protected they'll behave just like if they're private. There's a table in the link I posted before that summarizes it.

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.