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) ...
Related
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 have some C code that has some structs that looks like this:
typedef struct my_library_a_t my_library_a_t;
typedef struct my_library_b_t my_library_b_t;
typedef struct my_library_c_t my_library_c_t;
struct my_library_a_t {
struct my_library_b_t {
int data;
struct my_library_c_t {
int data;
} c;
} b;
int data;
};
This doesn't work in C++, because in C struct my_library_b_t defines a global struct my_library_b_t, whereas in C++ it defines ::my_library_a_t::my_library_b_t.
How can I get the inner struct definition to define a global struct in C++? Or even just not have to change too much code for it to work (I don't mind having a #ifdef __cplusplus block, but I don't want to pull the structs out because the reason they are nested in the first place is that they are used only one time each, and it's really hard to read when the inner class definitions are above the outer class definitions)
I tried struct ::my_library_b_t in the definition, but this doesn't work.
For context, I'm parsing a string that has a definition that would look like this:
a = b "," data
b = data "," c
c = data
data = %x31-39 *DIGIT ; 1-9 followed by *(0-9)
/ "0"
And the intermediate parts have meaning, so it's useful to be able to have functions that take my_library_b_t* or my_library_c_t*.
I would prefer to have a solution that looks like this:
#ifdef __cplusplus
#define GLOBAL_STRUCT(name) ??? (I tried `:: name`)
extern "C" {
#else
#define GLOBAL_STRUCT(name) name
#endif
struct my_library_a_t {
struct GLOBAL_STRUCT(my_library_b_t) {
// ...
I recommend this
struct my_library_a_t {
struct my_library_b_t {
int data;
struct my_library_c_t {
int data;
} c;
} b;
int data;
};
#ifdef __cplusplus
typedef struct my_library_a_t::my_library_b_t my_library_b_t;
typedef struct my_library_b_t::my_library_c_t my_library_c_t;
#else
typedef struct my_library_a_t my_library_a_t;
typedef struct my_library_b_t my_library_b_t;
typedef struct my_library_c_t my_library_c_t;
#endif
Notice that after alias my_library_b_t, you don't need to use my_library_a_t::my_library_b_t::my_library_c_t
I've got multiple classes from multiple engineers which I am using and they have the same named structures in the classes. From this I get the error "'struct' type redefinition". How do I get around this?
Example:
// Eng1Class.h
#pragma once
struct Eng1And2SameName
{
unsigned int bottle;
};
class Eng1Class
{
public:
Eng1Class();
~Eng1Class();
};
.
// Eng2Class.h
#pragma once
struct Eng1And2SameName
{
float x, y;
};
class Eng2Class
{
public:
Eng2Class();
~Eng2Class();
};
.
// Main Program
#include "stdafx.h"
#include "Eng1Class.h"
#include "Eng2Class.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Error: error C2011: 'Eng1And2SameName' : 'struct' type redefinition
According to this Compile error "'struct' type redefinition" although it's the first definition for it the #pragma once should fix the issues, but I still see the error. Any insights you can provide?
No, #pragma once prevents the header files from being included more than once - each is included once -> redefinition.
they have the same named structures in the classes
*header files
The're not defined inside the classes (nested), but they could be:
class Eng1Class
{
public:
struct Eng1And2SameName
{
unsigned int bottle;
};
Eng1Class();
~Eng1Class();
};
Or you could enclose the contents of those headers into two differently named namespaces.
Defining a namespace would help
For example as you said error with same struct definition in same namescope .
Reports error
You can do it by defining namesapce
#include<iostream>
using namespace std;
namespace Eng1 {
struct Eng1And2SameName
{
unsigned int bottle;
};
}
namespace Eng2
{
struct Eng1And2SameName
{
float x, y;
};
}
int main()
{
Eng1::Eng1And2SameName a;
Eng2::Eng1And2SameName b;
return 0;
}
Usually engineers working on the same product are coordinated somehow, at least they will use a common source code repository and a common build. Hence, conflicts should have come up earlier.
"uncoordinated" engineers may happen when they work on different products, and if so, each product could have its own namespace. Thereby, you can combine the products without having conflicts:
// in a header:
namespace Eng1Class {
struct Eng1And2SameName
{
unsigned int bottle;
};
class EngClass
{
public:
EngClass();
~EngClass();
};
}
// in the cpp-file
Eng1Class::EngClass::EngClass() {
cout << "hello, Class 1";
}
// in another (or even the same) header
namespace Eng2Class {
struct Eng1And2SameName
{
float x, y;
};
class EngClass
{
public:
EngClass();
~EngClass();
};
}
// in another (or even the same) cpp-file
Eng2Class::EngClass::EngClass() {
cout << "hello, Class 2";
}
I'm implementing some ideas using templates and static members. Although the "real" code produces another error, this is the one which i still have on a toy example code
#include <string>
#include <iostream>
template<int dim>
class Class1 {
public:
Class1() {};
~Class1() {};
void foo() {
std::cout<<"foo-1"<<std::endl;
}
protected:
std::string name;
};
template<int dim>
class Class2 : public Class1<dim>
{
public:
Class2(const int & a, const int &b) :
number( Class2<dim>::id_generator++ )
{
Class1<dim>::name = "My-name";
foo(); // (1)
};
void foo() {
Class1<dim>::foo();
std::cout<<"foo-2"<<std::endl;
}
private:
const unsigned int number;
static unsigned int id_generator;
};
int main()
{
int a = 1, b=2;
Class2<2> class2(a,b); // (2)
}
with the linker error:
undefined reference to `Class2<2>::id_generator'
rea-life example produces 2 errors
(1) required from 'Class2<dim>::Class2(int, int) [with int dim = 3]'
(2) required from here.
that real-life errors tell me absolutely nothing at all! :(
I hope if the toy-problem is solved, the real-life one will be gone too,
but if anyone has any ideas on the "real-life" errors (those 2 lines) in the context of the structure, pls, let me know.
You forgot to add a definition for your static data member id_generator. Add this at global namespace level:
template<int dim>
unsigned int Class2<dim>::id_generator = 0;
With this addition, you can see your code correctly compiling and linking here.
Well, as the error message says, there is no definition of the static data member. If this was an ordinary class, you'd put the definition in a source file:
// header:
class C {
static int i;
};
// source:
int C::i = 3;
A template is a pattern; the compiler uses it to generate code. So what you want to end up with when the compiler instantiates the template is something like the preceding code. But template code goes in headers, not source files, so you write it like this:
// header:
template <class T>
class C {
static int i;
};
template <class T>
int C<T>::i = 3;
When I make my own struct, say:
struct myStruct{
int data1;
int data2;
string data3;
}
I can initialize an instance of type myStruct like this:
myStruct instance1;
So my question is, why am I often seeing "struct" written during the initialization of a struct?
Maybe that's an inaccurate statement so here is an example of what I mean:
/*This is a tiny program that checks
to see if a file exists in
the current working directory. */
#include <sys/stat.h>
#include <iostream>
using namespace std;
const string FILENAME = data.txt;
int main(){
struct stat fileStatus; //<-- HERE HERE HERE!
if (FileExists(FILENAME, fileStatus)){
cout << "File Does Exist" << endl;
}else{
cout << "File Does NOT Exist" << endl;
}
return 0;
}
bool FileExists(const string & fileName,struct stat & fileStatus){
bool fileDoesNotExist = stat (fileName.c_str(), &fileStatus);
return !fileDoesNotExist;
}
>
LINE 13: struct stat fileStatus;
Is this something that was done in C for some reason?
Something with a macro or a typedef?
I just don't understand why this is the way it is.
This is a C thing; there's no good reason to continue to do it in C++.1
In C, struct is part of the typename, e.g.:
struct foo { int x; };
defines a type called struct foo. In C++, it defines a type called foo. In C, you can usually hide this irritation behind a typedef:
typedef struct foo { int x; } foo;
1 At least, not in code that couldn't possibly also be compiled as C (such as the example in your question).
You can do what you want by instead calling it like this:
typedef struct mystruct
{
int itema;
int itemb;
Etc...
}* mystruct;
So that's whenever you make a mystruct item it creates a pointer to your struct, else you have to call your struct by
struct mystruct *object;