#include <iostream>
#include <queue>
using namespace std;
int main () {
struct process {
int burst;
int ar;
};
int x=4;
process a[x];
queue <string> names; /* Declare a queue */
names.push(a[1]);
return 0;
}
I'm trying to pushing struct variable in queue but its not taking it and gives errors
no matching function for #include queue and invalid argument
how can I do that?
C++ is a strongly typed language. In the line names.push(a[1]); you are trying to push a struct (from your process a[x]; array) into a queue<string>. Your struct is not a string, so the compiler will emit an error. You at least need a queue<process>.
Other issues: variable length arrays are not standard C++ (process a[x];). Use a std::vector<process> instead. Here is some simple example that works:
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main () {
struct process // move this outside of main() if you don't compile with C++11 support
{
int burst;
int ar;
};
vector<process> a;
// insert two processes
a.push_back({21, 42});
a.push_back({10, 20});
queue <process> names; /* Declare a queue */
names.push(a[1]); // now we can push the second element, same type
return 0; // no need for this, really
}
EDIT
Locally defined classes/structs used to instantiate templates are valid only in C++11 and later, see e.g. Why can I define structures and classes within a function in C++? and the answers within. If you don't have access to a C++11 compliant compiler, then move your struct definition outside of main().
Related
What are the right methods?
How to avoid the 3 errors?
I tried the followings:
#include <vector>
#include <array>
#include <iostream>
using namespace std;
struct s_4{double x,z,k,wsize;};
typedef vec4 vector <array<double,4>>; //ERROR #1
void main()
{
vector <s_4> s1;
vector <array<double,4>> d1;
s1.push_back(*new (s_4){10.0,11,1,0.25e-3}); //OK
d1.push_back(*new (array<double,4>){10.0,11,1,0.25e-3}); //OK
d1.push_back(*new (double[4]){10.0,11,1,0.25e-3}); //ERROR #2
vector <array<double,4>> d2{11,12,13,14.1}; //ERROR #3
getchar();
}
It is like it is very difficult to use large arrays in vectors
The correct code is:
#include <vector>
#include <array>
#include <iostream>
using namespace std;
struct s_4{double x,z,k,wsize;};
typedef vector <array<double,4>> vec4;
int main()
{
vector <s_4> s1;
vector <array<double,4>> d1;
s1.push_back({10.0,11,1,0.25e-3});
d1.push_back({10.0,11,1,0.25e-3});
d1.push_back({10.0,11,1,0.25e-3});
vector <array<double,4>> d2{{11,12,13,14.1}};
return 0;
}
Your first error in the typedef is that the name of the typedef comes last.
Your first three push_backs were leaking memory, you don't need to name the type when initialising.
The second error is because a c array can't be converted directly to a std::array.
The last needs two sets of braces, one to initialise the vector and one to initialise each array.
In addition to Alan's answer:
Why are you trying to allocate your arrays on the heap? You could place your arrays on the stack and use initializer lists:
#include <vector>
#include <array>
#include <iostream>
int main()
{
std::vector <std::array<double,4>> data = {
{10.0,11,1,0.25e-3},
{10.0,11,1,0.25e-3},
{10.0,11,1,0.25e-3},
{11,12,13,14.1}
};
}
However, initializer lists are a C++11 feature so you may compile with -std=c++11:
g++ -g -Wall -O2 -std=c++11 test.cpp -o test
Furthermore you should avoid using namespace std, as this may cause problems if you use additional libraries that implement for example vectors for mathematical calculations.
I need to construct a crawler working with front_queues and back_queues, which are vectors of queues. I've seen solutions in this question Vector of queues but my compiler complains the vec needs a constructor.
#include <vector>
#include <queue>
using namespace std;
vector<queue<int> > vec;
vec.push_back( queue<int>(0) );
// ^
// error: expected constructor, destructor, or type conversion before ‘.’ token
You need to put function calls inside blocks.
Try adding a main function :
#include <vector>
#include <queue>
using namespace std;
int main()
{
vector<queue<int> > vec;
queue<int> q;
vec.push_back(q);
return (0);
}
queuedoesn't have initializer list :
According to queue's constructor reference (Source), you can't use queue<int>(0) because no proper constructor would match.
However, you can use queue<int>(). It will create an empty queue.
Take a look at this online example : https://ideone.com/RbT1pD
I have a problem with passing dynamically allocated structures to a function and accessing it's content.
The program uses mex to pass data from Matlab to C++. I use Visual Studio.
The structure I define in a header in 'InOut.h'
#include <string>
#include <cstdint>
#include <cstdlib>
struct sObjects {
std::string Type;
float *Position;
};
typedef struct sObject sObject;
In the main function I than allocate the structure is in 'MainFcn_Mex.cpp'
#include "MainFcn_Mex.h"
// ...
// get number of Objects from Matlab
int N_Obj = mxGetNumberOfElements(prhs[1]);
sObjects *Objects = new sObjects[N_Obj];
for (int k=0; k<N_Obj; k++) {
// get the pointer pointer map
pMap = mxGetField(prhs[1],k,"Type");
Objects[k].Type = mxArrayToString(pMap);
// get the pointer pointer map
pMap = mxGetField(prhs[1],k,"Position");
// setting pointer to the first Element
Objects[k].Position = (float*)mxGetPr(pMap);
mexPrintf("Objects(%d,1).Type: %s \n", k+1, Objects[k].Type);
}
create_Objects(Objects, N_Obj);
The function create_Objects is in a differente file 'create_Objects.cpp' and included via 'MainFcn_Mex.h':
#include <stdio.h>
#include <direct.h>
#define _USE_MATH_DEFINES
#include "math.h"
#include <cmath>
#include "mex.h"
#include "matrix.h"
#include <cuda.h>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include "InOut.h"
void create_Objects(sObjects *Objects, int N_Obj);
The content of 'create_Objects.cpp' so far is:
#define _USE_MATH_DEFINES
#include "math.h"
#include <cmath>
#include "InOut.h"
#include "mex.h"
void create_Objects(sObjects *Objects, int N_Obj)
{
for (int k=0; k<N_Obj; k++) {
mexPrintf("Objects(%d,1).Type: %s \n", k+1, Objects[k].Type);
}
}
Visual Studio tells me:
"error C2676: binary '[' : 'sObjects' does not define this operator or
a conversion to a type acceptable to the predefined operator"
Why can I access the data in the main function and not in seccondary functions?
How can I access a dynamically allocated structure in other functions, when its size isn't known at compile time?
Thanks a lot for your time!
It looks like you are trying to use struct directly as a typedef. Simply add typedef to your struct definition, will turn it into a type.
Like this:
... #include "mex.h"
typedef struct sObjects {
std::string Type;
float *Position;
};
(Otherwise you should use the full struct keyword as in void create_OpticsObjects(struct sObjects &Objects, int N_Obj).)
Your function prototypes don't need the extern qualifier.
You don't need extern unless you want globals variables. You seem to want simply global structs or types in your example, so extern is not required.
Using extern for globals
What you could be referring to is an instance of your object (or a pointer to your object), and that can be made global by using extern. as in this excerpt from the header file:
... #include "mex.h"
typedef struct sObjects {
std::string Type;
float *Position;
};
extern sObjects *pointerToOnesObjects;
Then in ONE source file, you need to declare the 'real' variable as in (this is good to initialise it here):
sObjects *pointerToOnesObjects = NULL;
With this method your variable pointerToOnesObjects is now available globally (in all your source files that use the same header file).
How do I map a key to a native data type like structure?
I wrote this snipped but I couldn't compile it. Do you have any ideas on how to fix it?
#include <map>
#include <iostream>
typedef struct _list
{
int a,b;
}list;
map<int,list> test_map;
int main(void)
{
cout <<"Testing"<< endl;
}
map resides in the std:: namespace. Two possible ways to fix this:
using namespace std;
// ...
map<int, list> test_map;
or
std::map<int, list> test_map;
I prefer the second method, but it's a purely personal choice.
On a related note, there is no real limitation on what you can put in a map, aside from the fact that they must be copyable/assignable, and that the key type must have a < operator (or you can also provide a comparer functor).
EDIT: Seems like <list> is included somewhere, either in <iostream> (unlikely) or <map> (strange but not impossible). A using namespace std will cause std::list to clash with your own struct. The solution: rename your struct, or remove the using namespace and put std:: where it's needed.
Added std where required.
Renamed list to mylist to avoid clash with std::list. Avoid typenames and variable names that clash with common usage.
Now compiles OK in VS2008.
#include <map>
#include <iostream>
typedef struct _list
{
int a,b;
} mylist;
std::map<int,mylist> test_map;
int main(void)
{
std::cout <<"Testing"<< std::endl;
return 0;
}
There's no issue with using your struct in the STL containers provided it's copyable cleanly (copy constructor), assignable (implements operator=) and comparable (implements operator<).
A number of problems here:
You're missing either a using::std or std::map, so the compiler doesn't know what map<int,list> means.
Assuming you have a using namespace std declaration, your typedef list might collide with the STL collection of the same name. Change the name.
Your typedef struct _tag {...} tag; construct is an archaic holdover from the 80's. It is not necesarry, and frankly rather silly. It gets you nothing.
Here's your code fixed:
#include <map>
#include <iostream>
struct MyList
{
int a,b;
};
std::map<int,MyList> test_map;
int main(void)
{
std::cout <<"Testing"<< std::endl;
return 0;
}
map<int, _list> test_map; or don't use list(much better) as a name of structure. (You probably also have
#include <list>
...
using namespace std;
somewhere in your code.
I would try to avoid using codepad at all.
I have done a couple of tests with your code and it seems that
it is adding an implicit (and unwanted) using namespace std --it does not require you to qualify map, cout or endl.
it is (probably) including more standard headers than you might want, including #include <list>.
That means that when the compiler looks at the code it is seeing two list, your version and the one in std. Because of the using directive, both are in scope in the line where you create the map and the compiler is not able to determine which to use.
Two simple things that you can do: change the name of your type for the simple test to something other than list (ouch! the tool forcing your naming choices!) or fully qualify the use:
#include <map>
struct list {
int a,b;
};
std::map< int, ::list > the_map;
// ...
Note that codepad is adding the include by itself and the using directive, so it will also compile:
struct list {
int a,b;
};
map<int,::list> the_map;
But that piece of code is wrong
You seem to be comming from C. Try this:
#include <map>
#include <iostream>
struct list
{
int a,b;
};
std::map<int,list> test_map;
int main(void)
{
std::cout <<"Testing"<< std::endl;
return 0;
}
Could any body offer me any reason about that?
If we do it like that, what's the outcome? Compile error?
The problem is that static initialization isnt just initialization, it is also definition. Take for example:
hacks.h :
class Foo
{
public:
static std::string bar_;
};
std::string Foo::bar_ = "Hello";
std::string GimmeFoo();
main.cpp :
#include <string>
#include <sstream>
#include <iostream>
#include "hacks.h"
using std::string;
using std::ostringstream;
using std::cout;
int main()
{
string s = GimmeFoo();
return 0;
}
foo.cpp :
#include <string>
#include <sstream>
#include <iostream>
#include "hacks.h"
using std::string;
using std::ostringstream;
using std::cout;
string GimmeFoo()
{
Foo foo;
foo;
string s = foo.bar_;
return s;
}
In this case, you can't initialize Foo::bar_ in the header because it will be allocated in every file that #includes hacks.h. So there will be 2 instances of Foo.bar_ in memory - one in main.cpp, and one in foo.cpp.
The solution is to allocate & initialize in just one place:
foo.cpp :
...
std::string Foo::bar_ = "Hello";
...
It is just a limitation in the language it self. Hopefully, when C++0x becomes reality, this limitation would go away.
I think this page gives a somehow good reason:
One of the trickiest ramifications of
using a static data member in a class
is that it must be initialized, just
once, outside the class definition, in
the source file. This is due to the
fact a header file is typically seen
multiple times by the compiler. If the
compiler encountered the
initialization of a variable multiple
times it would be very difficult to
ensure that variables were properly
initialized. Hence, exactly one
initialization of a static is allowed
in the entire program.