在课堂上做一个结构
Making a structure in class
我正在迈出学习OOP的第一步。这是我不能解决的第一个问题。此类中的max函数应返回最多两个数字。我想将数字保留在私有范围内,将函数保留在公共范围内。但是,当我想在公共作用域中使用struct data{}
中的变量时,编译器会说这些变量没有声明。请告诉我为什么会出现这些错误。
class myclass{
private:
struct data{
int q ;
int w;
};
public:
void get(int a, int b){
struct data = {a , b}; // here I want to pass the variables to data struct
}
int max (){ // this function returns the biggest number
if(q>w)
return q;
else
return w;
}
};
struct data{
int q ;
int w;
};
仅声明类型,而不是对象。因此,在class
实例中的任何位置都没有q
和w
成员。您需要声明struct
:的实例
struct {
int q;
int w;
} data;
然后,您可以将max
写为:
int max()
{
if (data.q > data.w)
return data.q;
else
return data.w;
}
(我不知道get
方法应该做什么,所以我没有替代品。)
在C++中,"class"和"struct"几乎是同义词(相同的东西)。唯一的区别是"struct"默认为"public"可访问性,而"class"默认为private。
一旦你理解了这一点,你所做的就是在你的类中定义一个子类型。
class myclass {
private: // <- not required, you already said that by saying "class".
struct data {
// <-- this is a class definition with "public:" just here.
...
};
};
C++允许嵌套类/结构定义,以便创建封送参数或返回值的结构。
class Database {
class Result { ... };
};
...
class Exam {
class Result { ... };
};
这两个结果类避免了名称空间冲突,因为它们是Database::result和Exam::result,而不仅仅是"result"。
然而,这些只是定义。如图所示,它们对外围类没有任何影响,也就是说:它们不会被用来向类中添加成员。
您的代码:
class myclass{
private:
struct data{ // <-- this is a TYPE declaration, struct myclass::data
int q ; //
int w; //
}; // <-- no member name here so does not affect myclass itself.
public:
void get(int a, int b){
struct data = {a , b}; // here I want to pass the variables to data struct
}
int max (){ // this function returns the biggest number
if(q>w)
return q;
else
return w;
}
};
声明类型"myclass::data",但不向该类添加类型"myclass::data"的成员。行"struct-data="是非法的,您正试图将值分配给TYPE。
可能应该写成
class MyClass {
int m_q;
int m_w;
public:
void set(int q, int w) {
m_q = q;
m_w = w;
}
int max() const {
return (m_q > m_w) ? m_q : m_w;
// or #include <algorithm> and return std::max(m_q, m_w);
}
};
你只需要举起q&如果你打算在类的范围之外重用该结构定义,例如在派生类或并行类中,你可能想添加更多相同的类型的东西,在这种情况下,你可能会做以下事情,但如果你这样做,你最终会因为破坏封装而自责:
class MyClass {
public:
struct Data {
int m_q;
int m_w;
};
private:
Data m_data;
void set(int q, int w) {
m_data.m_q = q;
m_data.m_w = w;
}
int max() const {
return (m_data.m_q > m_data.m_w) ? m_data.m_q : m_data.m_w;
}
};
如果这种成员的耦合需要在某种程度上从外部可见,那么更好的方法是:
class MyClass {
public:
class Data {
int m_q;
int m_w;
public:
Data() : m_q(0), m_w(0) {}
Data(int q, int w) : m_q(0), m_w(0) {}
void set(int q, int w) {
m_q = w;
m_w = w;
}
int q() const { return m_q; }
int w() const { return m_w; }
int max() const { return (m_q > m_w) ? m_q : m_w;
};
private:
Data m_data;
public:
MyClass() : m_data() {} // or = default
MyClass(int q, int w) : m_data(q, w) {}
MyClass(const Data& data) : m_data(data) {}
// Read-only access
const Data& data() const { return m_data; }
// To allow write access, e.g. for set:
Data& data() { return m_data; }
};
对于这样一个简单的案例来说,这有点过头了,但欢迎使用C++:样板语言。
您已经定义了结构,但没有该类型的对象。您应该声明一个对象,这样就不会出现任何错误。
class myclass{
private:
struct data{
int q ;
int w;
}var;
public:
void get(int a, int b){
var .q= a;
var.w=b; // here I want to pass the variables to data struct
}
int max (){ // this function returns the biggest number
if(var.q>var.w)
return var.q;
else
return var.w;
}
};
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 如何将数据从一个结构链接到另一个结构
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- std::transform 将一个结构数组复制到另一个结构数组
- 输入 ctrl + x 后如何再次 cin (cin<<x) 循环(cin 一个结构)
- 如何在级别上打印树,给定一个结构,该结构保存节点的值及其父节点的值,对于树中的每个节点
- 创建一个结构的关联数组,以创建一个缓冲区,供键快速访问
- 如何在另一个结构中初始化结构数组?
- 在另一个结构中声明内部结构会导致错误:结构使用无效
- 我想重置一个C++结构统计,我可以以某种方式使用 stat() 语法吗?
- C++可以输出一个结构的内存地址吗
- 通过 C++ 中的另一个结构成员访问结构
- C++ push_back() 一个结构体到一个向量中
- 难以从另一个结构中的函数返回结构
- 如何将向量添加到结构中以创建一个库存系统,在该系统中,我可以仅使用一个结构向系统添加多种不同的葡萄酒
- 如何制作一个结构程序,在其中可以存储无限量的数据,以便每次您想要时都可以将另一个产品添加到列表中?
- 将一个结构的值复制到另一个结构
- 是否可以在同一未创建的结构内创建一个结构