including a std::map within a struct? Is it ok? - c++

class X_class{
public:
struct extra
{int extra1;
int extra2;
int extra3;
};
enum a
{
n,m};
struct x_struct{
char b;
char c;
int d;
int e;
std::map <int, extra> myExtraMap;
};
};
in my code I define :
x_struct myStruct;
why do I get compile errors compiling the above class? The error either says:
1) expected ; before < on the line --- where I defined the map (above) if I eliminate std::
or
2) error: invalid use of ::; error: expected ; before < token

Probably you get erorrs because you didn't #include <map>

Related

How to initialize string in Class in c++ (g++ )

I'm getting this error while initializing a string in a class
Error :
publicclass.cpp:13:6: error: array type 'char [50]' is not assignable
s.n = "Randomstring";
But char is working. Only getting error with strings
#include<iostream>
using namespace std;
class student
{
public:
int ht;
char n[50];
};
int main()
{
student s;
s.ht = 1;
s.n = "Randomstring";
cout<<"Hallticket no : "<<s.ht<<"\n";
cout<<"Name : "<<s.n<<"\n";
return 0;
}
Compiler version is g++ 8.0.0 (getting same error with clang++ ,turboc++(in windows))
Arrays are not assignable.
You can initialise the member when you initialise the object that contains it:
student s{1, "Randomstring"};
You can copy the elements of an existing array after initialisation:
std::strncpy(s.n, "Randomstring", std::size(s.n));

How to use a vector of structs in a vector structs in c++?

I want to create a vector of trains, where each train needs a vector of pairs.
If I run the code outside of main(), I get these errors:
naive-bayes.cpp:17:15: error: template argument 1 is invalid
vector<pair> pairs;
naive-bayes.cpp:17:15: error: template argument 2 is invalid
Inside main(), I get these errors:
naive-bayes.cpp:22:15: error: template argument for 'template<class>
class std::allocator' uses local type 'main()::pair'
vector<pair> pairs;
naive-bayes.cpp:22:15: error: trying to instantiate 'template<class> class std::allocator'
naive-bayes.cpp:22:15: error: template argument 2 is invalid
Here is the code:
struct pair {
int index;
int value;
};
struct trains {
string label;
vector<pair> pairs;
};
You're problem is probably due to using namespace std;.
There is a std::pair type in the standard library.
Try this:
#include <string>
#include <vector>
struct pair {
int index;
int value;
};
struct trains {
std::string label;
std::vector<pair> pairs;
};
int main()
{
return 0;
}
Without a full program example to play with, all I can really point out is that your local pair declaration is likely getting confused with std::pair. Change your definition of struct pair to be struct mypair.

C++ Forward Declaration Error using Struct in Class as well as Constructor and Header

I have posted this already one day ago but I did not know how to add a second question to my first question.
I get a forward declaration error. You told me that it should be no problem if I define my class in KdTree.h and my functions, structs, etc in KdTree.cpp. However it does not work so here I post my whole code:
This is my header:
#Includes <iostream>
#Includes others
using namespace TooN;
#ifndef KDTREE_H_
#define KDTREE_H_
class KdTree {
public:
KdTree(std::vector<TooN::Vector<3,GLfloat> > & ,size_t);
struct node;
struct temptask;
struct temphold;
struct ...;
double function(...);
...;
std::vector<node> nodes;
std::vector < int > searchInRadius(const TooN::Vector<3, GLfloat> &,float , const std::vector<TooN::Vector<3,GLfloat> > & );
};
#endif
So and this is my KdTree.cpp:
#include "KdTree.h"
KdTree::KdTree(std::vector<TooN::Vector<3,GLfloat> > & points, size_t pointssize){
const size_t stacksize = 200;
nodes.push_back(node());
temphold tasksarray[stacksize] = {0,pointssize-1,0,0};
int taskindex = 0;
...A lot more stuff
if (!is_leaf(n)){
do something;
}
}
And then my functions in KdTree.cpp
struct KdTree::node{
std::size_t a, b, c;
node() : a(-1), b(-1), c(-1) {}
bool is_leaf(const node &n){
return blablabla;
}
};
And here my first 3 Compiler messages:( :
jni/Visual/KdTree.cpp: In constructor 'KdTree::KdTree(const std::vector<TooN::Vector<3, float> >&, size_t)':
jni/Visual/KdTree.cpp:32:23: error: invalid use of incomplete type 'struct KdTree::node'
nodes.push_back(node());
^
In file included from jni/Visual/KdTree.cpp:8:0:
jni/Visual/KdTree.h:29:9: error: forward declaration of 'struct KdTree::node'
struct node;
^
jni/Visual/KdTree.cpp:33:54: error: elements of array 'KdTree::temphold tasksarray [200]' have incomplete type
temphold tasksarray[stacksize] = {0,pointssize-1,0,0}; //starting at firstpoint = 0 index, lastpoint = lastindex, nodenumber = 0 index, dim = x-dimension (i.e. 0)
And a lot more of these kind of messages.
The struct definitions must still occur before their first use in the .cpp file.
#include "KdTree.h"
struct KdTree::node{
std::size_t a, b, c;
node() : a(-1), b(-1), c(-1) {}
bool is_leaf(const node &n){
return blablabla;
}
};
KdTree::KdTree(std::vector<TooN::Vector<3,GLfloat> > & points, size_t pointssize){
...A lot more stuff
}
Put the implementation of node at the beginning of KdTree.cpp, or at least before its first use:
#include "KdTree.h"
struct KdTree::node {
// ...
};
KdTree::KdTree() {
// ...
}

struct without typedef - cannot use in a pointer-to-member

I have a simple structure and I want a pointer-to-member c. I'm using MSVC2012 and if I don't declare the struct abc as a type definition (typedef), I can't use it.. how come?
struct abc
{
int a;
int b;
char c;
};
char (struct abc)::*ptt1 = &(struct abc)::c; // Error: error C2144: syntax error : 'abc' should be preceded by ')'
typedef struct abc;
char abc::*ptt1 = &abc::c; // Compiles just fine
if I don't declare the struct abc as a type definition (typedef), I can't use it.. how come?
You can, and you don't need the struct keyword, nor the typedef. Just do this:
char abc::*ptt1 = &abc::c;

What does the 'hides constructor for' warning mean when compiling C++ with g++?

Using the following code:
#include <stdio.h>
struct my_struct {
int a;
int b;
my_struct();
};
my_struct::my_struct(void)
{
printf("constructor\n");
}
void my_struct(void)
{
printf("standard function\n");
}
int main (int argc, char *argv[])
{
struct my_struct s;
s.a = 1;
s.b = 2;
printf("%d-%d\n", s.a, s.b);
return 0;
}
I get a warning compiling with g++ -Wshadow main.cpp:
main.cpp:15:20: warning: ‘void my_struct()’ hides constructor for ‘struct my_struct’
I would be ok with that warning if the void my_struct function actually replaced the my_struct::my_struct one. But it does not appears to be the case. If I run the program, I get:
constructor
1-2
Any idea what this warning mean ? It is quite annoying especially when I include C headers into C++ code
The warning points out that the my_struct() function has the same name as the my_struct structure. It means you will be unable to write:
my_struct s; // Error.
Because the compiler will think that you're using a function as a type. However, as you probably realized, you can still instantiate your structure with the struct keyword:
struct my_struct s; // Valid.
void my_struct(void) has the same name of your class/struct and since it is in the global namespace it is conflicting with your class/struct's constructor.
You could try something like:
#include <cstdio>
struct my_struct {
int a;
int b;
my_struct();
};
my_struct::my_struct(void)
{
printf("constructor\n");
}
namespace mbonnin
{
void my_struct(void);
}
void mbonnin::my_struct(void)
{
printf("standard function\n");
}
int main (int argc, char *argv[])
{
my_struct s;
s.a = 1;
s.b = 2;
printf("%d-%d\n", s.a, s.b);
mbonnin::my_struct();
return 0;
}
And by the way the struct in struct my_struct s; is redundant in C++.
warning: ‘void my_struct()’ hides constructor for ‘struct my_struct’
Any idea what this warning mean ?
It means that sometimes the warnings issued by the GNU compiler suite are a bit off. (Try omitting the semicolon after the close brace on the definition of struct my_struct. If you are using anything but a very recent version of g++ the error message will be a bit off.)
Something is being hidden here, but it is not the constructor for struct my_struct. What is being hidden is the name my_struct as a type identifier. You can see this in action if you remove the struct from the declaration of the variable s: Use my_struct s; instead of struct my_struct s; Without the contextual information offered by the struct keyword, the compiler now must interpret my_struct as a function name.