0

from a similiar question here I tried the solution

class A {
 ...
  public:

    // Give read-only access to vec_A
    const std::vector<DataStruct> & getVector() const {
        return vec_A;
    }
};

but always get error: 'DataStruct' was not declared in this scope. DataStruct and vec_A are defined in private section , below public section, in same class.

Please, could someone help me.

Regards, Thomas

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks for answer. My understanding was that normally public functions can acess private members ? If I make the whole DataStruct public I can use A.DataStruct and don't need a function to acess ? – Aquarius58 May 29 '20 at 06:51
  • 1
    Please include a [mcve]. The problem is in code you did not show (or rather in the code you did show, `DataStruct` is indeed not defined) – 463035818_is_not_an_ai May 29 '20 at 06:54
  • 1
    You need to [edit] your question to show us a [mcve]. Also please include a copy-paste (as text) of the full and complete build output. And please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude May 29 '20 at 06:54
  • Here my example came from, and I thought it's just what I need: https://stackoverflow.com/questions/45479457/access-private-member-vector-of-multiple-objects-in-other-class – Aquarius58 May 29 '20 at 06:54

3 Answers3

1

I suppose you have code similar to the following example:

#include <iostream>
struct foo {
    private:
       struct bar {
           void barbar() { std::cout << "hello";}
       };
    public:
    bar foobar() { return bar{}; }
};

int main() {
    foo f;
    foo::bar x = f.foobar();
    x.barbar();
}

It has an error:

<source>: In function 'int main()':
<source>:13:10: error: 'struct foo::bar' is private within this context
   13 |     foo::bar x = f.foobar();
      |          ^~~
<source>:4:15: note: declared private here
    4 |        struct bar {
      |               ^~~

because bar is private in foo. However, that doesnt mean that you cannot use it outside of foo. You can use auto:

int main() {
    foo f;
    auto x = f.foobar();
    x.barbar();
}

Or decltype:

int main() {
    foo f;
    using bar_alias = decltype(f.foobar());
    bar_alias x = f.foobar();
    x.barbar();
}

You cannot access the name DataType but you can use auto and you can get an alias for the type. This also works for a std::vector<DataType>, only some more boilerplate would be required to get your hands on DataType directly.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

You are trying to create a vector out of the Datatype "DataStruct". Are you sure that you wrote the Class/or implemented it? It could be only a variable. You know that you actually have to put a Datatype in it like int,bool,string. It defines what datatype the specific variables in the vector are made of.

Toast
  • 31
  • 9
0

Firstly, the the return type declarator part std::vector<DataStruct> requires a complete type in this context. That's what that error signifies. Class definition block can look forward for identifiers and signatures of members , but it cannot look for type definition.

Class A is not complete until its closing brace. Consecutively, nested classes defined aren't complete until theirs closing braces. Following declaration is correct one:

class A 
{
private:
    struct DataStruct {
    }; // a complete type
public:
    // Give read-only access to vec_A
    const std::vector<DataStruct> & getVector() const {
        return vec_A;
    }
private:   
   std::vector<DataStruct> vec_A;
};

In any other case you could forward declare it as struct A::DataStruct.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • 1
    once the type appears in a return type of a public method it is part of the public interface (whether the type is first declared in the private section or not) – 463035818_is_not_an_ai May 29 '20 at 06:57
  • [Why can I use auto on a private type?](https://stackoverflow.com/q/13532784/580083) – Daniel Langr May 29 '20 at 07:01
  • @idclev463035818 good point, I guess, I was used to broken compiler which couldn't do that (specifically a MS one, it was a non-compliance mess with "attempting" to access non-trivial constructor. Old MS compilers considered all classes with inheritance or nested to be non-trivial by definition and declaration "auto x" would require a call it could not make because "type is not accessible"). – Swift - Friday Pie May 29 '20 at 07:21
  • 1
    I wasnt aware of this for long time and it was quite a shock when I found this out. Had to reconsider my idea of what is private and what is public completely – 463035818_is_not_an_ai May 29 '20 at 07:25