I'm a C++ beginner and trying to make a nested struct which has 2 sub-structs under it.
The code is:
struct Sub_number{
int one;
int two;
};
struct Sub_size{
int width;
int height;
};
struct MainStruct{
struct Sub_number number;
struct Sub_size size;
}main;
and I got [Cannot use dot operator on a type] error from Xcode when I tried to put a value in it like this:
main.number.one = 13;
^
Does anyone has any ideas what's wrong with this code...?
Thank you so so much everyone. As you wrote, the name I've using was the no-good point!! Silly me.. I'll double check when I'm going to ask on StackOverflow next time.
Thanks!
main is the reserved word for main function (starting point of application) you need to change the variable name to something else . This will fix the issue
struct Sub_number {
int one;
int two;
};
struct Sub_size {
int width;
int height;
};
struct MainStruct {
struct Sub_number number;
struct Sub_size size;
}someVariable;
void main() {
someVariable.number.one = 1;
}
your struct name can't be main .main is a unique function name int main().change the struct name to others!
Related
I tried this code after documenting myself :
struct person
{
int a;
char s;
};
struct person test;
test.a = 12;
And Code::Blocks returns this following error :
error: 'test' does not name a type
Can someone explain this error to me? I found this sample code on the internet! I don't understand my mistake.
Thanks for reading, have a nice day.
To make your code work you need to put it in a function and there should be a main function too.
struct person
{
int a;
char s;
};
int main() // you need to have a main
{
// code needs to be in a function
/*struct*/ person test; // struct is not needed
test.a = 12;
return 0;
}
I have a doubt regarding structs and if statements in C++
For the sake of simplicity, I have created a sample code to explain my intention
int var = 10
struct example{
int a;
int b;
if(var > 8){
int c;
}
};
I have a codebase which uses similar kind of code as above. Commenting out the if portion does not give any errors.
My question is
Could if statements be put in struct declarations?
If not, what is the possible remedy for this, since if statment is mandatory.
Note: I cannot use #if,#else directives nor std::optional or other standard libraries to mitigate this, so please help me find another solution.
No you can't use if statement inside your struct or class definition. Instead, for condition declaration, you can use #if directive.
#define var 10
struct example {
int a;
int b;
#if var > 8
int c;
#endif
}
This will work.
'#if' is a compiler directive (pre-processor directive). if statement on the other hand is runtime statement.
Other than this there is no other way!
int main()
{
int var = 10;
if(var>10){
struct example{
int a;
int b;
int c;
};
}else {
struct example{
int a;
int b;
};
}
cout<<"Hello World";
return 0;
}
There are multiple ways to address your problem one way of achieving is shown.
We cannot add if Statement in the Structure.
i'm a iOS programer from china, I'm so sorry that i can't make an exact title for this question, but i'll try to describe it detailed. If there are any one can help me to change the title, i'm very thankful about that. Sorry for my bad English.
When i using clang -rewrite-objc to see the source code about the Block Syntax, i found there is something that i can't understand. Here is my code:
int main(int argc, char *argv[]) {
void (^blk)() = ^ {
};
blk();
}
And the core source code is
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
}
static struct __main_block_desc_0 {
size_t reserved;
size_t Block_size;
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};
int main(int argc, char *argv[]) {
void (*blk)() = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);
}
In the main function, when i call the blk(), the source code cast blk and take the FuncPtr by this code
((__block_impl *)blk)->FuncPtr)
I can't really understand that, is it supposed to do? In my opinion, i prefer to use
(((__main_block_impl_0 *)blk ->impl).FuncPtr)
I don't really know more about C++, if there is anyone who can help me to understand the principle of this code, i'll be very thankful. Thanks for you guys.
Well, struct __main_block_impl_0's first member (impl) is a struct __block_impl. So the location of the struct __main_block_impl_0 is the same as the location of the struct __block_impl that is its first member. If you have the pointer to one you can just treat it as a pointer to the other.
I want to put constants in a struct, but compiler generates error that ";" is missing at the "=".
struct {
int aaa=111; // error: "expected ; at end of declaration list"
} blah;
Did you try maybe:
int aaa{111};
And if you need int as a constant you should probably include the const keyword.
const int aaa{111};
You can't initialize at the time of defining the structure in Obj-C. Initialization is possible at the time of creating instance shown as below.
struct Employee {
int idNumber;
int age;
};
// create instance
struct Employee emp1;
emp1.idNumber=12345;
emp1.age = 25;
If you're using Objective-C, in an .h file add something like:
extern const struct MyStruct {
int aaa;
} MyStruct;
In the .m file:
const struct MyStruct MyStruct = {
.aaa = 1
};
#import the .h file and use the struct in your code like this:
if (someInteger == MyStruct.aaa) ...
so ive been doing c++ for a little while now but im wondering how to use a struct in a class, lets say i was making a FPS game i created a basic structure for a gun:
struct gun
{
int damage;
string name;
int number_of_bullets;
};
and i created a class for a enemy like this:
class enemy
{
const int max_health = 100;
int health;
int damage;
gun mgun;
};
when i compile the program i get a error that sais: 'gun' does not name type.
what am i doing wrong? thanks.
1) you have to define "gun" before your class.
2) put a semicolon at end of struct "gun"
struct gun
{
int damage;
string name;
int number_of_bullets;
};
3)in your class , the "const int max health = ...." is wrong put an '_' between "max" & "health" or something else.