为结构赋值 [英] Assigning values to a struct

查看:51
本文介绍了为结构赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在从第三方移植一些代码,并且已经遇到以下问题: ba
a问题:


typedef struct {

unsigned char Red;

unsigned char Green;

unsigned char Blue;

} TBoxColour;


// -------------------------- -------------------------------------------------

无效DoSomething(无效)

{

TBoxColour Col;


Col =(TBoxColour){ 10,20,30};

}


我的编译器(CodeGear C ++ Builder)抱怨错误:


不正确使用typedef''TBoxColour''

似乎完全合乎逻辑的是为结构赋值

成员,但这是合法的,还是原来的程序员只是在某个特定的编译器中利用一些怪癖(不知道哪个)?


谢谢

推荐答案

开启8月29日,上午12:59,Alf P. Steinbach, < al ... @ start.nowrote:
On Aug 29, 12:59 am, "Alf P. Steinbach" <al...@start.nowrote:

尝试


BoxColor const color = {10,20,30 };


干杯,& hth。,


- Alf
Try

BoxColor const color = { 10, 20, 30 };

Cheers, & hth.,

- Alf



你好Alf,


对不起,我应该说,这个TBoxColour的一个实例在整个代码中的100个位置分配了新的值。


我想是唯一的方式是:


Col.Red = 10;

Col.Green = 20;

Col.Blue = 30;


原始方法看起来更优雅,这只是丑陋,但更多

重要的是我用一个简单的<改变代码并不容易br />
搜索和替换。

Hi Alf,

Sorry, I should have said, a single instance of this TBoxColour gets
assigned new values in 100''s of locations throughout the code.

I suppose the only way is:

Col.Red = 10;
Col.Green = 20;
Col.Blue = 30;

The original method looks more elegant, This is just ugly, but more
importantly it''s not easy for me to change the code with a simple
search and replace.


Cliff写道:
Cliff wrote:

On 8月29日,上午12:59,Alf P. Steinbach, < al ... @ start.nowrote:
On Aug 29, 12:59 am, "Alf P. Steinbach" <al...@start.nowrote:

>尝试

BoxColor const color = {10,20,30};

干杯,& hth。,

- Alf
>Try

BoxColor const color = { 10, 20, 30 };

Cheers, & hth.,

- Alf



你好Alf,


对不起,我应该说,这个TBoxColour的单个实例在整个代码中的100个位置分配了新值。


我想唯一的方法是:


Col.Red = 10;

Col.Green = 20;

Col.Blue = 30;


原始方法看起来更优雅,这只是丑陋,但更多

重要的是我用一个简单的改变代码并不容易

搜索和替换。


Hi Alf,

Sorry, I should have said, a single instance of this TBoxColour gets
assigned new values in 100''s of locations throughout the code.

I suppose the only way is:

Col.Red = 10;
Col.Green = 20;
Col.Blue = 30;

The original method looks more elegant, This is just ugly, but more
importantly it''s not easy for me to change the code with a simple
search and replace.



你可以定义一个函数(一个伪构造函数):

BoxColor createBoxColor(int a,int b,int c){

BoxColor bc = {a,b,c};

返回bc;

}


然后在你需要指定的地方使用它:


Col = createBoxColor(10,20,30);


(我明白我在没有阅读剩下的帖子的情况下插手,

对不起;如果我写的是虚假的,请原谅我并忽略它。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回应top-发表回复,请不要问

You can define a function (a pseudo-constructor):

BoxColor createBoxColor(int a, int b, int c) {
BoxColor bc = { a, b, c };
return bc;
}

and then use it anywhere you need to assign:

Col = createBoxColor(10, 20, 30);

(I understand that I barge in without reading the rest of the thread,
sorry for that; if what I wrote is bogus, forgive me and ignore it)

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




Cliff< us ****** @ btconnect.comwrote in message .. 。

Cliff <us******@btconnect.comwrote in message...

8月29日上午12:59,Alf P. Steinbach < al ... @ start.nowrote:
On Aug 29, 12:59 am, "Alf P. Steinbach" <al...@start.nowrote:

尝试

BoxColor const color = {10,20,30};

干杯,& hth。,Alf
Try
BoxColor const color = { 10, 20, 30 };
Cheers, & hth., Alf



你好Alf,


对不起,我应该说,这个TBoxColour的一个实例得到

在整个代码中的100'位置分配了新值。

我想唯一的方法是:


Col.红色= 10;

Col.Green = 20;

Col.Blue = 30;


原始方法看起来更优雅,这只是丑陋的,但是更多

重要的是我用一个简单的

搜索和替换来改变代码并不容易。


Hi Alf,

Sorry, I should have said, a single instance of this TBoxColour gets
assigned new values in 100''s of locations throughout the code.
I suppose the only way is:

Col.Red = 10;
Col.Green = 20;
Col.Blue = 30;

The original method looks more elegant, This is just ugly, but more
importantly it''s not easy for me to change the code with a simple
search and replace.



嗯,因为你不会说,也许这会把它拖出你:

struct BoxColor {

unsigned char红色;

unsigned char绿色;

unsigned char蓝色;

BoxColor():红色(0),绿色(0),蓝色( 0){}

BoxColor(unsigned char R,unsigned char G,

unsigned char B):红色(R),绿色(G),蓝色(B){ }

void颜色(unsigned char R,unsigned char G,

unsigned char B){

红色= R;绿= G;蓝色= B;

返回;

}

};


//如果你不能更改此结构,请参阅BoxColor2

typedef struct {

unsigned char Red;

unsigned char Green;

unsigned char蓝色;

} TBoxColour;


struct BoxColor2:TBoxColour {

BoxColor2(){Red = 0;绿色= 0;蓝色= 0; } $ / $
BoxColor2(unsigned char R,unsigned char G,

unsigned char B){

Red = R;

Green = G;

Blue = B;

}

};

int main(){

BoxColor bc1(10,20,30);

cout<<" bc1 Red ="<< int(bc1.Red)<< ;"绿色="

<< int(bc1.Green)<<"蓝色="<<< int(bc1.Blue)<< std :: endl;

BoxColor bc2;

cout<<" bc2 Red = QUOT;<< INT(bc2.Red)LT;<"绿色="

<< int(bc2.Green)<<"蓝色="<<< int(bc2.Blue)<< std :: endl;

bc2.Color(40,50,60);

cout<<" bc2 Red ="<< int(bc2.Red)<<""绿色="

<< int(bc2.Green)<<" Blue ="<< int(bc2.Blue)<< std :: endl;


BoxColor2 BC2(10,20,30);

TBoxColour TBC;

TBC = BC2;

cout<<"TBC Red ="<< int(TBC.Red)<< ;"绿色="

<< int(TBC.Green)<<" Blue ="<< int(TBC.Blue)<< std :: endl;


TBC = BoxColor2(30,40,50);

cout<<"" TBC Red ="<< int(TBC.Red)<<<"绿色="

<< int(TBC.Green)<<"蓝色="<<< int(TBC.Blue)<< std :: endl;

// ----------------- -------------------

返回0;

} // main()


那,或者使用Victor的例子。


-

Bob R

POVrookie

Well, since you won''t say, maybe this will drag it out of you:
struct BoxColor{
unsigned char Red;
unsigned char Green;
unsigned char Blue;
BoxColor() : Red(0),Green(0),Blue(0){}
BoxColor( unsigned char R, unsigned char G,
unsigned char B ) : Red(R), Green(G), Blue(B){}
void Color( unsigned char R, unsigned char G,
unsigned char B ){
Red = R; Green = G; Blue = B;
return;
}
};

// if you cannot change this struct, see BoxColor2
typedef struct{
unsigned char Red;
unsigned char Green;
unsigned char Blue;
}TBoxColour;

struct BoxColor2 : TBoxColour{
BoxColor2(){ Red = 0; Green = 0; Blue = 0; }
BoxColor2( unsigned char R, unsigned char G,
unsigned char B ){
Red = R;
Green = G;
Blue = B;
}
};
int main(){
BoxColor bc1( 10, 20, 30 );
cout<<"bc1 Red="<<int(bc1.Red)<<" Green="
<<int(bc1.Green)<<" Blue="<<int(bc1.Blue)<<std::endl;
BoxColor bc2;
cout<<"bc2 Red="<<int(bc2.Red)<<" Green="
<<int(bc2.Green)<<" Blue="<<int(bc2.Blue)<<std::endl;
bc2.Color( 40, 50, 60 );
cout<<"bc2 Red="<<int(bc2.Red)<<" Green="
<<int(bc2.Green)<<" Blue="<<int(bc2.Blue)<<std::endl;

BoxColor2 BC2( 10, 20, 30 );
TBoxColour TBC;
TBC = BC2;
cout<<"TBC Red="<<int(TBC.Red)<<" Green="
<<int(TBC.Green)<<" Blue="<<int(TBC.Blue)<<std::endl;

TBC = BoxColor2( 30, 40, 50 );
cout<<"TBC Red="<<int(TBC.Red)<<" Green="
<<int(TBC.Green)<<" Blue="<<int(TBC.Blue)<<std::endl;
// ------------------------------------
return 0;
} // main()

That, or use Victor''s example.

--
Bob R
POVrookie


这篇关于为结构赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆