cannot access private member declared in class (header file)

Hi everyone...

I am currently doing the assignment about linked list. Here are some details information about what I am doing..

This program is C++ and should run on Visual Studio 2010.
And it contains three file, two datastructure header and one main cpp file.

This program is trying to arrange and show some sports records. The main program which contain the functions such as reading the result text file(each result text file contain several records of athletes), removing a file, arranging the totalresult and printing it out. And the main program is already given and I cannot overwrite it.

But when I finished and try to build the solution and run it, I am not able to run the program and it give me somethings like these...

warning C4172: returning address of local variable or temporary
error C2248: 'Datastructure1::Datastructure1' : cannot access private member declared in class 'Datastructure1'
see declaration of 'Datastructure1::Datastructure1'
see declaration of 'Datastructure1'
This diagnostic occurred in the compiler generated function 'Result::Result(const Result &)'

And I have tried to comment each function part of the header file and see if can run or not. But I still fail to do so.

Here are my codes...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef DATASTRUCTURE1_H
#define DATASTRUCTURE1_H

class Datastructure1 {
    
 
public:
   
  Datastructure1( );
  ~Datastructure1( );


  int size( ){
      int i=0;
          while(result[i]!= NULL){
              i++;
          }
          return i;      
    
  }

  bool add( int new_elem ){
      for(int i=0;i<size();i++){
          if(result[i]==NULL){
              result[i] = new_elem;
              return true;
          }
      }
        return false;

  }

  int& operator []( int index ){
      if(index>0 && index<(size()+1) ){
        return result[index-1];
      }
      int a =0;
      return (int &)a;
  }

private:
    int result[20];
  // copying forbidden
  Datastructure1(const Datastructure1&);
  // no assignment
  Datastructure1& operator=(const Datastructure1&);

};

#endif 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef DATASTRUCTURE2_H
#define DATASTRUCTURE2_H


#include "datastructure1.h"


using namespace std;

struct Result {

  string name;
  int totalresult;
  Datastructure1 results;
  Result *next;
};

class Datastructure2 {

 
public:


  Datastructure2( );

  ~Datastructure2( );

  int size( ){
      int count=0;
      Result* p = head;
        while (p != NULL)
        {
            ++count;
            p = p->next;
        }
        return count;

  }


  bool add( Result* new_r, int index ){
      Result *temp;
      temp = head;

    for(int c=1;  c<(index-1); c++){
          temp = temp->next;
    }
    if(temp->next!=NULL){
        Result *temp2;
        temp2=temp->next;  
        temp->next = new_r;
        temp->next->next=temp2;
        return true;
    }else if(temp->next==NULL){
        temp->next = new_r;
        return true;
    }else{return false;}
    

  }


  Result& operator[]( int index ){
    int i;
    Result *currPtr;
    currPtr = head;
    for(i = 1; i < index; i++){
        currPtr = currPtr->next;
    }
    return  *currPtr;

    
       
  }

  void remove( int index ){
    int i;
    Result *currPtr;
    currPtr = head;
    for(i = 1; i < index; i++){
        currPtr = currPtr->next;
    }
        currPtr->next = currPtr->next->next;
    

  }

  void removeAll( ){
      Result* temp1 = head;
        while(temp1!=NULL)
            {   
                head->next = temp1->next;
                temp1->next = NULL;
                delete(temp1);
                temp1 = head->next;
            }
  }
 
private:
    Result *head;
    Result *tail;
    


  // copying forbidden
  Datastructure2(const Datastructure2&);
  // no assignment
  Datastructure2& operator=(const Datastructure2&);

};

#endif 



There are two header files and look quite long. They are all some linked list functions . I have read and learn linked list data structure before I complete this programs. However, when I complete the functions required, the function cannot be compile....
Please help me and I have worked on this for whole weeks already..
thank you again
The problem is you've made Datastructure1 non-copyable, but are trying to make Result copyable - despite the fact that Result contains an instance of Datastructure1

When your compiler creates a copy constructor for Result - which it has to do - it needs to use the copy constructor for Datastructure1, so that the results member gets copied. Since you've made the copy constructor for Datastructure1 private, it can't be accessed by Result, so the compilation fails.

Possible solutions:

1) Write your own copy constructor for Result, that doesn't depend on calling the private copy constructor for Datastructure1.

2) Prevent copying of Result too.

3) Make Datastructure1 copyable.
Last edited on
oh thanks man. I am new in C++. But I have to add Datastructure1 results in the Result data structure as it is part of this. May you suggest me which way is the best which not affecting whole program.
And may you also give me some guides or help on the above methods please?
Well, it's a design decision that you have to make.

Do you want Result to be copyable? If so, then you'll need to decide how to implement that.

If you don't want it to be copyable, then make Result non-copyable just like you've made Datastructure1 non-copyable.
Last edited on
Oh, I see what you mean.
The Datastructure1 is copying forbidden but I need to put this data structure(Datastructure1) to the Result because it is one important part of it.
Result is a data structure which include several information and Datastructure1 is part of it (an integer array). And How can I do to achieve this with no copyable situation?
Or the only way I can do is make it copyable?
thank you
Do you want Result to be copyable?
what is the purpose of being copyable?
I want to use the Result data structure to store the several information of each person(to store all things of one object). In this case, should my result be copyable too?
I am a little confused about this even I have read the tutorial.
Thank you for answering me again
Well, what were your reasons for making Datastructure1 non-copyable? Do those reasons also apply to Result?
When i delete the Datastructure1(const Datastructure1&); in Datastructure1.h, error occurs. So I think Datastructure1 must be non-copyable. Is there any way I can do to achieve the same goals I want?

Here are the main program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
int main( ) {                  
	
  
  Datastructure2 tr2;

  char command = ' ';

  while( true ) {

    cin >> command;
    
    // addition
    if( command == 'L' ) {
      
      string file_name = "";
      cin >> file_name;
      
      // read file
      if( !addFile( tr2, file_name ) ) {
	
	cout << "Error in reading file: " << file_name 
	     << endl;
      }
    }
    // removal
    else if( command == 'P' ) {
      
      string file_name = "";
      cin >> file_name;
      
      // read the file
      if( !removeFile( tr2, file_name ) ) {
	
	cout << "Error in opening file: " << file_name
	     << endl;
      }
    }
    
    // print
    else if(  command == 'K' || command == 'T') {
      
      int amount = 0;
      if( !readNumber( amount ) || amount <= 0 || amount > tr2.size( ) ) {
	
	cout << "Incorrect amount!" << endl;
      }
      else if( command == 'K' ) {
	
	print(tr2, amount);
      }
      else {
	printListing(tr2, amount);
      }
    }
    

    // quit
    else if( command == 'Q' ) {
      
      tr2.removeAll();
      return EXIT_SUCCESS;    
    }
    
    else {
    
      cout << "Unknown command: " << command << endl;
    }
  }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
bool addFile( Datastructure2& tr2, string& file_name ) {

  // open the file
  ifstream file( file_name.c_str( ) );

  // file not found
  if( !file ) {

    return false;
  }

  char separator =' ';
  int index = 1;
  string smudge;
  // Create Result structs out of the lines in the file
  while( !file.eof() ) {
    
    Result* new_result = new Result;
    getline(file, new_result->name, ':');

    file >> new_result->totalresult;
    int tmp =0;

    for( int idx = 0; idx < THROWS; idx++ ) {

      file >> separator >> tmp;
      new_result->results.add( tmp );
    }

    while(isspace(file.peek())) {
      file.get(separator);
    }
    
    while(index <= tr2.size() && 
	  tr2[index].totalresult > new_result->totalresult) {

      index++;
    }
    // add new result to database
    tr2.add( new_result, index );
  }
  return true;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
bool removeFile( Datastructure2& tr2, string& file_name ) {

  // open file
  ifstream file( file_name.c_str( ) );

  // file not found
  if( !file ) {

    return false;
  }

  char separator = ' ';
  int index = 1;
  string name = "";
  int totalresult = 0;

  // Go through the file
  while( !file.eof() ) {
    
    getline(file, name, ':');

    file >> totalresult;
    int tmp = 0;

    for( int idx = 0; idx < THROWS; idx++ ) {

      file >> separator >> tmp;
    }
    
    while(isspace(file.peek())) {
      file.get(separator);
    }    

    while(index < tr2.size() && 
	  tr2[index].totalresult > totalresult) {

      index++;
    }

    // remove the resultfrom the data
    if( tr2[index].name == name) {

      tr2.remove( index );
    }
  }
  return true;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
void print( Datastructure2& tr2, int amount ) {

  for( int index = 1; index <= amount; index++ ) {

    cout << tr2[index].name << " " << tr2[index].totalresult;

    for( int jndex = 1; jndex <= THROWS; jndex++ ) {
    
      cout << " " << tr2[index].results[jndex];
    }
    cout << endl;
  }
}


1
2
3
4
5
6
7
void printListing( Datastructure2& tr2, int amount ) {

  for( int index = 1; index <= amount; index++ ) {

    cout << tr2[index].name << " " << tr2[index].totalresult << endl;
  }
}


1
2
3
4
5
6
7
8
9
10
11
12
bool readNumber( int& number) {

  string tmp = "";
  cin >> tmp;
  istringstream ss(tmp);

  if(!(ss >> number)) {

    return false;
  }
  return true;
}




When i delete the Datastructure1(const Datastructure1&); in Datastructure1.h, error occurs. So I think Datastructure1 must be non-copyable.

Well, I'm going to assume you understand what that error is, and that you know why it's happening. So you therefore understand why Datastructure1 must be non-copyable.

From looking at your code, I don't see anywhere where you're actually trying to copy Result objects. So why are you so reluctant to make Results non-copyable too?
Datastructure2 is initialed in int main(). Is it not a copying operation?
If I want to make Result data structure non-copyable too, what should I do to achieve it...
As the default setting is that the datastructure1.h is non-copyable, and struct Result include this "Datastructure1 results; " in it. Is there any possible way to do it such as a data structure that can accessing and avoiding copying.

And what is the purpose to make the headers non-copyable?
Topic archived. No new replies allowed.