struct

Structure tag and name, why does a local variable declared as name compile?

大憨熊 提交于 2020-01-13 09:07:48
问题 In some code I saw recently there was a structure defined like this: typedef struct tagMyStruct { int numberOne; int numberTwo; } MYSTRUCT; The way I understand this, tagMyStruct is the new data type and MYSTRUCT is a variable that is created right there. At another place, this was used like this: MYSTRUCT *pStruct = new MYSTRUCT; and it compiled fine with Visual Studio 2010. How is that valid C++? I thought MYSTRUCT was a variable and not a type? 回答1: No. tagMyStruct is the name of the

Do I kill a kitten each time I use struct everywhere instead of class?

筅森魡賤 提交于 2020-01-13 09:04:52
问题 struct is public by default while class is private by default. Lets take Ogre3D for example; if I change all class occurences with struct, it compiles (I guess), and the engine works just as before. If I'm right, the compiled code is exactly the same as before, because it's only the compiler that does check if a private/protected methods are called, it's not checked at runtime. If I'm still right, class is just a keyword that just makes its cute eyes and begging "please encapsulate your data:

C++ Non Template Method in Template Class

戏子无情 提交于 2020-01-13 08:12:03
问题 Is it posible to write implementation of non template method in template class(struct) at .cpp file? I have read that template method should be written on .h, but my method is not template method although it belongs to template class. Here is the code in my .h: #include <iostream> #ifndef KEY_VALUE_H #define KEY_VALUE_H using namespace std; namespace types { template <class T, class U> struct key_value { T key; U value; static key_value<T, U> make(T key, U value) { key_value<T, U> kv; kv.key

C++ Non Template Method in Template Class

北战南征 提交于 2020-01-13 08:10:29
问题 Is it posible to write implementation of non template method in template class(struct) at .cpp file? I have read that template method should be written on .h, but my method is not template method although it belongs to template class. Here is the code in my .h: #include <iostream> #ifndef KEY_VALUE_H #define KEY_VALUE_H using namespace std; namespace types { template <class T, class U> struct key_value { T key; U value; static key_value<T, U> make(T key, U value) { key_value<T, U> kv; kv.key

Why doesn't the compiler generate compile errors if an incorrect argument type is passed to a struct initialiser list?

烂漫一生 提交于 2020-01-12 11:10:36
问题 I have defined a struct, which has a constructor: struct MyStruct { MyStruct(const int value) : value(value) { } int value; }; and the following objects: int main() { MyStruct a (true); MyStruct b {true}; } But I haven't received any compile errors, either with MVS2015 or Xcode 7.3.1. Why am I not getting any compile errors? How do I make the compiler help me detect this? (Initially, the struct was written to have bool data, but after some time, code changed and bool became int and several

Why doesn't the compiler generate compile errors if an incorrect argument type is passed to a struct initialiser list?

浪尽此生 提交于 2020-01-12 11:09:32
问题 I have defined a struct, which has a constructor: struct MyStruct { MyStruct(const int value) : value(value) { } int value; }; and the following objects: int main() { MyStruct a (true); MyStruct b {true}; } But I haven't received any compile errors, either with MVS2015 or Xcode 7.3.1. Why am I not getting any compile errors? How do I make the compiler help me detect this? (Initially, the struct was written to have bool data, but after some time, code changed and bool became int and several

Why doesn't the compiler generate compile errors if an incorrect argument type is passed to a struct initialiser list?

扶醉桌前 提交于 2020-01-12 11:09:03
问题 I have defined a struct, which has a constructor: struct MyStruct { MyStruct(const int value) : value(value) { } int value; }; and the following objects: int main() { MyStruct a (true); MyStruct b {true}; } But I haven't received any compile errors, either with MVS2015 or Xcode 7.3.1. Why am I not getting any compile errors? How do I make the compiler help me detect this? (Initially, the struct was written to have bool data, but after some time, code changed and bool became int and several

where to declare structures, inside main() or outside main()?

偶尔善良 提交于 2020-01-12 08:09:50
问题 Case 1: structure declared outside main() working fine #include<stdio.h> #include<conio.h> struct prod { int price,usold; }; int main() { struct prod *p,a; int billamt(struct prod *); int bill; printf("enter the values \n"); scanf("%d%d",&p->price,&p->usold); bill=billamt(p); printf("bill=%d",bill); getch(); } int billamt(struct prod *i) { int b; b=(i->price*i->usold); return b; } Case 2: declared inside main() giving error [Error] type 'main()::prod' with no linkage used to declare function

Size of structures in .NET

谁说我不能喝 提交于 2020-01-12 07:16:28
问题 My problem is to send a structure between a program in C to a C# program. I made a structure in C#: public struct NetPoint { public float lat; // 4 bytes public float lon; // 4 bytes public int alt; // 4 bytes public long time; // 8 bytes } The total size of the structure must be 20 bytes. When I do a sizeof() in C++ of this structure, System.Diagnostics.Debug.WriteLine( "SizeOf(NetPoint) = " + System.Runtime.InteropServices.Marshal.SizeOf(new NetPoint())); the debug console shows: SizeOf

C Unknown type name 'my_structure'

青春壹個敷衍的年華 提交于 2020-01-12 07:15:49
问题 I have this code: main.h #ifndef MAINH #define MAINH ... #include "my_struct.h" void some_func(my_structure *x); ... #endif and my_struct.h #ifndef UTILSH #define UTILSH ... #include "main.h" ... typedef struct abcd { int a; } my_structure; ... #endif but I'm getting this when I try to compile: error: unknown type name ‘my_structure’ Any idea why? 回答1: Because of how you've ordered your includes, the compiler sees void some_func(my_structure *x); before it sees typedef struct abcd { int a; }