C++ 在课堂上制作结构
我正在学习OOP的第一步。这是我无法解决的第一个问题。 此类中的max函数最多应返回两个数字。我想将数字保留在私有范围内,将函数保留在公共范围内。但是当我想在公共范围内使用C++ 在课堂上制作结构,c++,class,constructor,C++,Class,Constructor,我正在学习OOP的第一步。这是我无法解决的第一个问题。 此类中的max函数最多应返回两个数字。我想将数字保留在私有范围内,将函数保留在公共范围内。但是当我想在公共范围内使用structdata{}中的变量时,编译器会说这些变量没有声明。请告诉我为什么会出现这些错误 class myclass{ private: struct data{ int q ; int w; }; public: void get(int a, int b){
structdata{}
中的变量时,编译器会说这些变量没有声明。请告诉我为什么会出现这些错误
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;
}
};
只声明类型,而不是对象,因此在类
实例中的任何位置都没有q
和w
成员。您需要声明结构的一个实例
:
struct {
int q;
int w;
} data;
然后,您可以将max
写成:
int max()
{
if (data.q > data.w)
return data.q;
else
return data.w;
}
(我不知道你的get
方法应该做什么,所以我没有替代方法。)
只声明类型,而不是对象,因此在类
实例中的任何位置都没有q
和w
成员。您需要声明结构的一个实例
:
struct {
int q;
int w;
} data;
然后,您可以将max
写成:
int max()
{
if (data.q > data.w)
return data.q;
else
return data.w;
}
(我不知道你的<代码>获取/代码>方法应该做什么,所以我没有替换它。)C++中“类”和“结构”的
< P>接近同义(相同的事物)。唯一的区别是,“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.
...
};
};
这两个结果类避免了名称空间冲突,它们是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;
}
};
如果这种构件耦合需要在一定程度上对外可见,则更好的方法是:
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; }
};
<> P>这类案例有点过头,但欢迎C++:样板语言。C++中“类”和“结构”的< P>是同义词(同一事物)。唯一的区别是,“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.
...
};
};
这两个结果类避免了名称空间冲突,它们是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;
}
};
如果这种构件耦合需要在一定程度上对外可见,则更好的方法是:
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;
}
};
您已经定义了结构,但没有该类型的对象。你应该声明一个对象,你不会得到任何错误
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;
}
};
你为什么要在课堂上这样做?我认为函数模板更合适。不管怎样,您都需要一个
数据
结构的实例作为myclass
的成员变量;不是作为get()
函数的局部作用域变量。可能是。。。或者……我在课堂上做这些只是为了看看和理解它是如何工作的。我知道这可以用一句话来写:你为什么要用一个类来做这件事?我认为函数模板更合适。不管怎样,您都需要一个数据
结构的实例作为myclass
的成员变量;不是作为get()
函数的局部作用域变量。可能是。。。或者……我在课堂上做这些只是为了看看和理解它是如何工作的。我知道它可以用一行来写:Dget
看起来应该改为set
)get
看起来应该改为命名为set
)np-修正了一些拼写错误等-希望他们澄清np-修正了一些拼写错误等-希望他们澄清