通过同一类的私有成员访问私有成员变量
access private member variable through private member, same class
// M9P369.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
const int MaxSize = 100;
using namespace std;
class Set {
int len; // number of members
char members[MaxSize]; // the set is stored in this array
int find(char ch); // find an element
public:
Set() { len = 0; } // constructor to make a null set initially
int getLength() { return len; } // return number of elements in the set
void showset(); // display the set
bool isMember(char ch); // check for membership
Set operator+(char ch); // overload operator to add an element to the set
Set operator-(char ch); // overload operator to remove an element from the set
Set operator+(Set ob2); // set Union - overloaded by the different type from above overload+ function
Set operator-(Set ob2); // set difference same as above.
};
// Return the index of the element passed in, or -1 if nothing found.
int Set::find(char ch) {
int i;
for (i=0; i < len; i++)
if (members.[i] == ch) return i;
return -1;
}
// Show the set
void Set::showset() {
cout << "{ ";
for (int i=0; i<len; i++)
cout << members[i] << " ";
cout << "}n";
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
我正在学习操作符重载,遇到一个类访问问题。
行if (members.[i] == ch) return i;
给出一个工具提示错误'表达式必须具有类类型',以及编译错误:
m9p369.cpp(34): error C2059: syntax error : '['
m9p369.cpp(40): error C2228: left of '.showset' must have class/struct/union
m9p369.cpp(41): error C2228: left of '.cout' must have class/struct/union
我正在定义类Set的私有成员函数find(),并且在试图访问同一类members的私有成员char数组时得到错误。错误似乎说我应该指定它引用的是哪个类,为什么?我已经在定义中指定了类:
int Set::find(char ch) {
据我所知,成员应该在函数定义的作用域中。我努力寻找任何多余的字符,我没有发现任何奇怪的,所有括号似乎都匹配。
问题在这里:
members.[i]
应该是
members[i]
从
中删除.
if (members.[i] == ch) return i;
~~~~~~~~~~~^
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 将包含C样式数组的对象初始化为成员变量(C++)
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 在循环中按顺序遍历成员变量
- c++类声明时,相同的例程,不同的成员变量类型
- 如何从另一个文件继承私有成员变量和公共函数
- 在C++类中,是否必须初始化作为数组的成员变量
- 如何从子成员函数修改父公共成员变量
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 从私有成员变量的成员方法返回unique_ptr
- 在派生类中使用基类的私有成员变量的最佳方法
- 静态 constexpr 类成员变量对多线程读取是否安全?
- C++:是否可以使用非静态成员变量模板?
- 打印所有继承的类成员变量和方法
- 如何在复杂继承中访问静态成员变量
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- 成员变量与函数概念检查