使用关键字,我做错了什么
using keyword, what i do wrong?
/* ..CODE.. */
struct st_Settings {
struct {
unsigned int x;
unsigned int y;
unsigned int width;
unsigned int height;
} window;
} defaultSettings;
/* ..CODE.. */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) {
using st_Settings; // default settings
{
using window;
{
x = 50;
y = 50;
width = 800;
height = 600;
}
}
/* ..CODE.. */
}
此代码不起作用,这是我的问题,我可以用其他与结构一起使用的东西替换"使用"关键字?
okey我有这样的结构:
struct
{
struct a
{
int a;
int b;
int c;
}
struct b
{
struct a
{
int a;
int b;
int c;
}
struct b
{
int a;
int b;
int c;
}
}
struct c
{
int a;
int b;
int c;
}
} a;
我必须这样做:
a.a.a = 1;
a.a.b = 12;
a.a.c = 14;
a.b.a.a = 41;
a.b.a.b = 61;
a.b.a.c = 34;
a.b.b.a = 65;
a.b.b.b = 45;
a.b.b.c = 23;
a.c.a = 1;
a.c.b = 0;
a.c.c = 4;
,或者可以做这样的事情:
a.
{
a.
{
a = 1;
b = 12;
c = 14;
}
b.
{
a.
{
a = 41;
b = 61;
c = 34;
}
b.
{
a = 65;
b = 45;
c = 23;
}
}
c.
{
a = 1;
b = 0;
c = 4;
}
}
using
关键字在您的代码中未正确使用(在代码的这一部分中根本不需要):
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
st_Settings settings;
settings.window.x = 50;
settings.window.y = 50;
settings.window.width = 800;
settings.window.height = 600;
// ...
}
还请注意,定义结构的方式是定义一个名为defaultSettings
的全局实例。如果您想使用它,则将删除上面settings
的行声明,并将其所有其他实例替换为defaultSettings
。
在声明变量时,您想要的唯一的东西是初始化结构:
struct st_Settings {
struct {
unsigned int x;
unsigned int y;
unsigned int width;
unsigned int height;
} window;
} defaultSettings = {{50,50,800,600}};
同时,您可以在功能中初始化:
int a = 10 ;
int b = 20 ;
struct st_Settings declaredAndSetted = {{a,b,defaultSettings.window.width,400}} ;
如果您使用C 0x标准($ g++ -std=c++0x <file>
),则可以以这种方式将值重新分配到结构变量:
defaultSettings = {{a,b,defaultSettings.window.width,400}} ;
相关文章:
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 我的C++线程做错了什么?
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- 没有输出的合并排序我做错了什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 在C++中使用 AKS 素数测试计算双胞胎素数 我做错了什么?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- FFMPEG,C++,内存泄漏,我做错了什么?
- 我做错了什么?反向字符串 C++
- 我在这个课上做错了什么?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 我在查找和字符串上做错了什么?
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 我在尝试将多个值push_back向量时出错。我做错了什么?
- 插入排序:我做错了什么?