Class and private member access

Here is an example from this site's page on classes which illustrates what I am trying to figure out. I have added a line in which I attempt to modify the string, which in this case hasn't been made private, but this does not work.

Question is: How to use the functions that are already included in string when the string is part of a class?

Will I have to design an entirely new function to carry out a task that can normally be automated?

In my case I have a class which contains a private vector, and would like to use vector functions like ".erase".

Thanks in advance.

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
  

// classes and default constructors
#include <iostream>
#include <string>
using namespace std;

class Example3 {
    string data;
  public:
    Example3 (const string& str) : data(str) {}
    Example3() {}
    const string& content() const {return data;}
};

int main () {
  Example3 foo;
  Example3 bar ("Example");
  //modify using string function
  int x = bar.length(); 
  cout << "bar's content: " << bar.content() << int x << '\n';
  return 0;
}


Separate example: 

void PQ::delete_used_point(int i, PQ priority_queue) { 
	priority_queue.erase(priority_queue(i)); 
//tried changing this [i] to brackets but same result.
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// classes and default constructors
#include <iostream>
#include <string>
using namespace std;

class Example3 {
    string data;
  public:
    Example3 (const string& str) : data(str) {}
    Example3() {}
    const string& content() const {return data;}
};

int main () {
  Example3 foo;
  Example3 bar ("Example");
  //modify using string function
  int x = bar.length(); 
  cout << "bar's content: " << bar.content() << int x << '\n';
  return 0;
}


I have added a line in which I attempt to modify the string, which in this case hasn't been made private, but this does not work.

I don't see any line of code here in which you have attempted to modify the string.


int x = bar.length();
This line makes no sense. bar is an object of type Example3, and you have tried to call the function Example3::length(), but there is no such function. The Example3 class does not have a member function length()


Question is: How to use the functions that are already included in string when the string is part of a class?


If you want to reach a public member variable, just use it:
1
2
 string newString = bar.data;
 int lengthOfString = bar.data.length(); 


If the class variable is private, then you made it private for a reason. Create public functions that do what's needed, and call those public functions.
Last edited on
Your question is senseless.
The string data is private, but you say it isn't.
You say you are "modifying" the string, but there is no attempt to do so.

If you just want to know how to get the length (aka size) of the private string then you would need to add a member function to Example3 to do it:

 
  size_t size() const { return data.size(); }

Without adding a size/length member function, a reference to a std::string is being returned so use one of the string's member functions to get the length/size:
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
#include <iostream>
#include <string>

class Example3
{
private:
   std::string data;

public:
   Example3(const std::string& str) : data(str) {}
   Example3() {}

   const std::string& content() const { return data; }
};

int main()
{
   Example3 foo;
   Example3 bar("Example");

   int x = foo.content().length();

   std::cout << "The length of foo is: " << x << '\n';

   std::cout << "The size of bar is: " << bar.content().size() << '\n';
}

This exposes the private data member too much, even though it is returned as a const, so writing a custom size/length member function may be necessary.
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
#include <iostream>
#include <string>

class Example3
{
private:
   std::string data;

public:
   Example3(const std::string& str) : data(str) {}
   Example3() {}

   size_t Size() const { return data.size(); }
};

int main()
{
   Example3 foo;
   Example3 bar("Example");

   int x = foo.Size();

   std::cout << "The length of foo is: " << x << '\n';

   std::cout << "The size of bar is: " << bar.Size() << '\n';
}
Last edited on
Thanks Furry Guy,

To be more adroit, I have a class that sets up a private vector of Point type. Point is another class with two coordinates that can be accessed. This is for the purpose of allowing a bunch of bots to access the vector and go to the points.

The first step in the program is to compile a list of points into a vector.

After the point becomes unnecessary, I want to delete it from the vector using the int at which it exists. However I don't know what I'm going to name my PQ class object, which complicates things in contrast to the above example.

I edited my function so it is declared within the class, modeling it after yours.

now I get this error:

error: type 'vector<Point>' does not provide a call operator
void delete_used_point(int i) {priority_queue.erase(priority_queue(i));



class PQ {
private:
vector<Point> priority_queue;
public:
void compile_list(Area &area);
void delete_used_point(int i) {priority_queue.erase(priority_queue(i));} //
double calculate_distance(int id, Loc loc); //id passed from Bot class?

};
Don't name a variable priority_queue, especially not in conjunction with
using namespace std
Since there is a class template named std::priority_queue in the standard library.

To erase an element by index from a vector:
v.erase(v.begin() + i)
priority_queue(i)

This suggests that you're just guessing at what functions a class provides. You don't have to guess (and probably shouldn't); you should look them up.
Topic archived. No new replies allowed.