Accessing private members

I've been reading the tutorials on Friendship and Inheritance (http://www.cplusplus.com/doc/tutorial/inheritance/) but I still don't understand why I can't access members of the same struct type.

1
2
3
4
5
6
7
8
9
10
11
 bool wordBeginsAt (int pos)
{
  if (pos == 0)
    return true;

  ///use the 'message' string
  Message go;

  return isAlphanumeric(go.messageText[pos]) && (!isAlphanumeric(go.messageText[pos-1]));

} 


The code above is located in a source file, where the function isAlphanumeric passes a char value, and Message is the struct containing the string I want to access. Below is the declaration of the struct and string located in the corresponding header file.

1
2
3
4
5
6
7
8
struct Message{
.
.
.
private:

  std::string messageText;
};


My frustration comes when I try to call and assign messageText like the tutorial does to its private members, but I keep getting an error saying I can't access the string because it is a private member. Is there a way to access the string without having to pass it through the function wordBeginsAt?
its a private member, so obviously you cannot access it.

either you need to make wordBeginsAt a member of Message struct and then access the private parts or make messageText as public.

something like this:

1
2
3
4
5
6
7
8
struct Message{
.
.bool wordBeginsAt (int pos);
.
private:

  std::string messageText;
};


1
2
3
4
5
6
7
8
9
10
 bool wordBeginsAt (int pos)
{
  if (pos == 0)
    return true;

  ///use the 'message' string

  return isAlphanumeric(messageText[pos]) && (!isAlphanumeric(messageText[pos-1]));

}


1
2
3
4
5
void main()
{
Message go;
go.wordBeginsAt ();
}


do you think, this is fine ?
Yeah! I'm sorry, bool wordBeginsAt (int pos); already exists int the struct, I just forgot it add it in the description. Your suggestion is actually the first thing I tried doing, however I get an error saying 'messageText' in function 'wordBeginsAt' was not declared in the scope. Do I have to declare something in the source file (besides #include "header.h") to access the string?
Did your definition of the function is correct?

like this:

bool Message::wordBeginsAt (int pos)
This is how it's set up

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef MESSAGE_H
#define MESSAGE_H

#include <string>
#include <iostream>

struct Message {
  .
  .
  .
  bool wordBeginsAt (int pos) const;
  .
  .
  .
private:

  std::string messageText;

};

#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
#include "message.h"

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
  .
  .
  .
bool wordBeginsAt (int pos) 
{
  if (pos == 0)
    return true;

  ///use the 'message' string

  return isAlphanumeric(messageText[pos]) && (!isAlphanumeric(messageText[pos-1]));

}
  .
  .
  .
///------------->end of source file  


I know that header file is correct because it was given that way. So whats wrong with the source file??
The source file needs Message:: in front otherwise you are not implementing a method but rather just defining a global function.
Oh, yeah. Sweet! Thanks!
Topic archived. No new replies allowed.