假设我存储了一个对象向量.该对象有一个属性,如int age,它是私有的,我们需要通过getAge()访问它.
让我们说我们把对象称为"人"
我将对象"people"存储在一个向量中... soo ...
vector<people> vectorOfPeople
Run Code Online (Sandbox Code Playgroud)
我的问题是......是否有可能通过矢量vectorOfPeople直接访问'age'?也许是类似的vectorOfPeople[0].getAge()东西......(我知道这不起作用).
谢谢!
你能帮帮我解决这个问题吗?
#include <iostream>
#include <cstring>
using namespace std;
class A
{
public:
char str[4];
A()
{
str = "C++";
cout << "Constructor A" << endl;
}
void display()
{
cout << str << " is your name" << endl;
}
};
int main()
{
A a;
a.display();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给出了以下错误:
**************************Error**********
StringProg.cpp:9: error: ISO C++ forbids initialization of member "str"
StringProg.cpp:9: error: making "str" static StringProg.cpp:9: error: invalid in-class initialization of static data member of non-integral type "char [4]" …Run Code Online (Sandbox Code Playgroud)