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;
}
Related
I tried the following code:
#include <iostream>
using namespace std;
int main()
{
char string[4]='xyz';
return 0;
}
Since string is a keyword the compiler should give error but it runs fine. Can anyone explain why it compiles successfully.
string is not a keyword.
It's the name of a type declared in the standard library.
When you give it a name, you're doing something called shadowing. This is more clear in the following example:
{
int x = 0;
{
int x = 5;
std::cout << x << std::endl;
}
std::cout << x << std::endl;
}
What gets printed?
Well, 5 first then 0.
This is because the x in the second scope overrides the x from the first. It "shadows" the first declaration.
This works with typenames as well:
struct MyStruct {
int x;
};
...
{
...
int MyStruct = 10;
...
}
Here, MyStruct gets overridden within that scope.
That same thing happens in your example with std::string
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.
Here's the code:
#include <iostream>
using namespace std;
class Zaix
{
private:
static int mor;
public:
static int beri;
static void setmor(int lip)
{
Zaix::mor=lip;
}
static int getmor(void)
{
return mor;
}
};
int Zaix::beri=3;
int main()
{
cout<<Zaix::beri<<endl;
Zaix::beri++;
cout<<Zaix::beri<<endl;
Zaix::setmor(6);
return 0;
}
Now, line 4 of main() function Zaix::setmor(6); somehow invalidates line 11 of the code presented Zaix::mor=lip;. With this line commented out, the whole thing compiles OK, with it present, compiler gives this error:
undefined reference to Zaix::mor"
Any idea why that is?
Define the variable outside class as well.
int Zaix::mor;
For assignment:
int Zaix::mor = 4;
In C++ we need to define all the static member variable of a class outside of it else we get a linking error. You just need to do like below:-
int Zaix::mor;// Just add this line below int Zaix::beri = 3;
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) ...
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!