Vector of structs with using structs out of other class - c++

I need to create and use vector with structs of other class.
So class Scanner contains following struct structure:
class Scanner
{
struct structOneScan
{
unsigned int X_MULTIPLE_POS[290];
unsigned int Z_MULTIPLE_POS[290];
unsigned int I_MULTIPLE_COUNT[290];
}
structOneScan SnapsArray[3000] ;
};
in Draw.cpp i manipulate this struct:
if (Counter<3000)
{
for(int i = 0; i < 290; i++)
{
Scanner.SnapsArray[Counter].X_MULTIPLE_POS[i] = (double) (Scanner.X_POS[i] * X_Factor);
Counter ++;
};
Now that i have the information copied in my SnapsArray, i need to analyse this information and save it in a vector, cause there can be many such Scans.
Thats why i create another class CMeasurementResult, to save the structs in a vector:
#include "CScanner.h"
#include <vector>
using namespace std;
class CMeasureResult
{
public:
vector<structOneScan*>Scans;
};
but the way i try it doesn´t work. Also tried it over a Pointer from Scanner, but it doesn´t work too.
vectorScans; doesn´t work too.
vectorScans; ist the declaration. Qualifier 'scanner' is not a name of a class or namespace. Tempaltespecification out of 'vector<_Ty,_Ax>' can´t be generated. Sorry for bad translation, my compiler is in German language. So if I compile, this ist the Error. C++ Builder Embarcadero

Related

How can I pass arguments to a class by creating an object in C++?

I am working on my first separate class file. I have been given a driver program which I am not supposed to change and I am to create a class file that runs off the driver.
#include <iostream>
#include <iomanip>
using namespace std;
#include "Question.h"
int main()
{
string q2Answers [] = {"China","India","Mexico","Australia"};
Question q2("Which country is home to the Kangaroo?",q2Answers,'D');
q2.display();
cout << endl;
}
The driver, simplified above, appears to be passing arguments to the class via arguments. My class header file is built in the following fashion.
#ifndef QUESTION_H
#define QUESTION_H
#include <string>
using namespace std;
class Question
{
public:
void setStem(string newStem);
void setAnswers(string newAnswers[]);
void setKey(char newKey);
void display();
string getStem();
string getAnswer(int index);
char getKey();
private:
string stem;
string answers[4];
char key;
};
#endif // QUESTION_H
How can I execute the functions in my class using arguments passed into an object? I am confused as to how the line,
Question q2("Which country is home to the Kangaroo?",q2Answers,'D');
has any way of pushing those arguments into the functions. Any insight into this would be very appreciated.
If I understood you correctly, you're asking for how to make a constructor(See OldProgrammer's link in the comment to your question):
You can either make it right in the header file, like so:
Question(const std::string& theQuestion,
const std::string theOptions[], const char& correctAnswer)
{
this->stem = theQuestion;
for(int i=0; i<4; i++){
this->answers[i] = theAnswers[i];
}
this->key = correctAnswer;
}
~Question(){}
//This is called the "Destructor", it is a function called when the object is destroyed
(You can imagine the const std::string&-part as string or const char&-part as just char if you don't know what they mean, since it's not very important right now.)
Or you can make it in the separate .cpp-file like so:
Question::Question(const std::string& theQuestion,
const std::string& theOptions[], const char& correctAnswer)
{
this->stem = theQuestion;
for(int i=0; i<4; i++){
this->answers[i] = theAnswers[i];
}
this->key = correctAnswer;
}
Question::~Question(){}
You might be asking why we use destructors; it's because sometimes there are things we need to do before the object is removed.
Like for instance if you want to save certain info or make a change, or more commonly to free dynamic memory you allocated when you made the object. Otherwise you get memory leaks, which are bad.
Then you can construct/create an object as such:
Question q2("Which country is home to the Kangaroo?",q2Answers,'D');
You can also "overload" the constructor, i.e. you can make other versions of it. For example if you imagine that the constructor's only parameter was the question:
Question(std::string q){
this->stem = q;
}
Question(char c[]){
this->stem = c;
}
Now you can pass either a string or an array of characters to the object. But if you only have one, you can't do the other too, so if we only have the first constructor, we can't pass an array of characters to do the exact same thing. And you can make as many of these as you like, but it doesn't necessarily mean that it's better just because it has a ton of constructors.

Is there a simple way to implement it in C++?

I want to implement a function that can print out the value of one member variable (for example, 'aa') of struct ('Data') by it's name.
I try to use the macro definition as follows, but failed.
Is there a simple way to implement it?
#include <string>
#include <iostream>
using namespace std;
struct Data
{
int aa;
int bb;
int cc;
Data(): aa(1),bb(2),cc(3) {};
};
#define Param(a,b) a.##b
void Process(Data& data, const string& name)
{
cout << Param(data, name) << endl;
}
void main()
{
Data data;
Process(data, "aa");//I want print the value of Data.aa
Process(data, "bb");//I want print the value of Data.bb
Process(data, "cc");//I want print the value of Data.cc
}
This is not possible in C++.
This kind of usage is generally seen in scripting languages.
In C++ the variable names are constructed at compile time.
Your original code sample makes no sense to me because if you call Param(name) then the compiler has to know what instance of Data it has to use to determine the value of the member variable you want to get the value of (but I'm neither an expert using macros nor do I like them very much).
I tried to solve your problem using the following approach:
struct Data
{
int aa;
};
#define GetMemberValue(d, n) d.##n
int main()
{
Data d;
d.aa = 3;
int i = GetMemberValue(d, aa);
}
At least this approach returns the right result in this case.
Another thing is that you stated that you cannot call the member variables directly i.e. data.aa so you might run into the same issue using the macro. It's just a guess as I don't know the original code you're using.

error: ‘length’ was not declared in this scope c++

I am beginner c++ programmer, It's my first program even (For those who are very keen to give negatives). I had written the same code in c but now trying to do in c++.
Where I get the following error.
error: ‘length’ was not declared in this scope
My code is as below.
#include <iostream>
#include <fstream>
#include <assert.h>
using namespace std;
class Huffman
{
public:
int data_size, length; //THis length variable is not accessible in main function below in main function.
Huffman(char *filename);
~Huffman();
struct Huffman1
{
int value;
unsigned char sym; /* symbol */
struct Huffman1 *left,*right; /* left and right subtrees */
}; typedef struct Huffman1 Node;
};
Huffman::Huffman(char * file_name)
{
//I will do something here soon
}
Huffman::~Huffman()
{
}
int main(int argc, char * * argv)
{
length=10; //Not accessible here.
if (argc < 2)
{
cout<<"Ohh.. Sorry , you forgot to provide the Input File please" <<endl;
return(0);
}
Huffman Object1(argv[1]);
return(0);
}
I am not sure that it's c++ programming error because it may be because i am compiling it g++ Filename.c -o filename. Could someone please correct if it's a programming error or it's due to the way i compile ?
thanks.
length is a member of the class, so it does not exist outside the class.
You can access lenth after creating an object of class Huffman as follows
Huffman Object(argv[1]);
Object.length = 10;
length belongs to Huffman class. So you should use it for Object1 after it's definition:
Huffman Object1(argv[1]);
Object1.length = 10;
You know, public: doesn't mean that anything put inside under that branch in the class tree, will be accessible everywhere it just means that you access the instance variables of the class through "dot notation" like so Object.length.
However if you truly wanted length to be accessible everywhere, you should declare it as a global variable:
short int length;
class Huffman{
...
};
...
It's a compile error and your code is responsible. You defined length inside your Huffman class. It's a member of that class, not a global variable.
Imagine your class as a C Struct. You'd need to create a struct first in order to access the variable. Same thing applies to C++ classes.
Try Object1.length = 10; after you create the instance of your class.
EDIT
For your purposes, use C++ classes as you would use C structs. That will do the trick.
I would actually put the Node struct declaration outside of the Huffman class. I think it's easier to understand. Also, using a typedef to a struct is not really that useful in C++ for these cases, the name of the struct is usable by just declaring the struct.
The pointers do not allocate memory for the struct themselves. Only after you allocate memory they will be usable, and even then they're members of Object1, so you need that too.
#include <iostream>
#include <fstream>
#include <assert.h>
using namespace std;
struct Node
{
int value;
unsigned char sym; /* symbol */
};
class Huffman
{
public:
int data_size, length; //THis length variable is not accessible in main function below in main function.
Huffman(char *filename);
~Huffman();
Node *left,*right; /* left and right subtrees */
};
Huffman::Huffman(char * file_name)
{
//I will do something here soon
}
Huffman::~Huffman()
{
}
int main(int argc, char * * argv)
{
length=10; //Not accessible here.
if (argc < 2)
{
cout<<"Ohh.. Sorry , you forgot to provide the Input File please" <<endl;
return(0);
}
Huffman Object1(argv[1]);
Object1.left = new Node;
Object1.right = new Node;
//Do your stuff here...
Object1.left->sym;
return(0);
}
This should get you started, it is by no means a perfect implementation. It's not even very C++ oriented, but I already went ahead of myself with the answer. This is a topic for a very different question, which you're welcome to ask in SO, but try not to make questions inside questions.
Good luck!
length is part of your class, not main, thus the compiler is right.
Members belong to an object and are accessed liek this:
Huffman huffmannObj(...);
std::cout << huffmannObj.length << std::endl;
length is a publicly accessible member of your class, but you'll need an instance of that class first before you can do anything with the member
Huffman h(whatever_constructor_params);
h.length = 10;
...is ok

Send to class char array defined in header

The issue that I am having is that I am trying to build a DLL. And I am using char instead of strings to store information.
I have defined the following in the header file:
class Test{
public:
int qLenght;
char firstName[];
char surName[];
};
I am having problems inputting codes from the main program using the following:
int main()
{
Test theTest;
theTest.firstName[0] = {"Mike Smith","Jonny Vegas","Jimmy Woo"};
}
I have included the header code at the top of my main project.
It won't let me add to the char array. This may seem like a stupid question but I am struggling and hopefully someone can shed some light as to where I am going wrong. Am I missing a parameter?
Your class needs to know how much memory to allocate when you instantiate the class (which is not the same time as you assign the values).
class Test
{
public:
char firstName[2][100];
};
int main()
{
Test theTest;
strcpy(theTest.firstName[0], "Mike Smith");
strcpy(theTest.firstName[1], "Jonny Vegas");
return 0;
}
Alternatively, you can allocate memory for the strings dynamically at the time of assignment, but then you need to remember to free it again:
class Test{
public:
char *firstName[2];
};
int main()
{
Test theTest;
theTest.firstName[0] = strdup("Mike Smith");
theTest.firstName[1] = strdup("Jonny Vegas");
// do stuff
free(theTest.firstName[0]);
free(theTest.firstName[1]);
return 0;
}

C++ vectors - acessing the size of vector of type struct declared within a struct

My declaration is as follows
#include <vector>
#include <iostream>
using namespace std;
typedef struct _ListofHops_T
{
int macAddrLtr;
int ttlValue;
}ListofHops;
struct ReadActivateLinkTrace
{
typedef std::vector < ListofHops *> ListofHopsList;
bool operationState;
};
int main()
{
ReadActivateLinkTrace readLinkTrace;
for (size_t listItr=0; listItr < readLinkTrace.ListofHopsList.size(); listItr++)
{
.....
}
}
I am trying to declare a vector of list of hops struct within a struct ReadActivateLinkTrace.
Is the above declaration valid.
I get the following error compiling
vector.cpp:23: error: invalid use of
ReadActivateLinkTrace::ListofHopsList
I am new to vectors . how can i acess/iterate through vector of structures defined in a structure?
The declaration is valid, but it doesn't do what you think. ListofHopsList is a type (hint: typedef), not a variable. You're probably looking for
struct ReadActivateLinkTrace
{
std::vector < ListofHops *> ListofHopsList;
bool operationState;
};
The problem wasn't with the vector itself, but with the fact that you weren't declaring a member, but defining a new type.
Also, is there any reason you're using a vector of pointers as opposed to a vector of objects?
ReadActivateLinkTrace::ListofHopsList is a typedef, which only declares an alias for a name of a type. It does not define an actual object of that type. You apparently want:
struct ReadActivateLinkTrace
{
std::vector < ListofHops *> ListofHopsList;
bool operationState;
};
You should probably have some second thoughts about that being a vector of pointers though. At least offhand, it doesn't seem very likely that a pointer is the best choice here. While you're at it, this:
typedef struct _ListofHops_T
{
int macAddrLtr;
int ttlValue;
}ListofHops;
Is pretty horrible in a couple of ways. First the typedef here is only needed in C code, not C++. Second, the name _ListofHops_T is reserved for the implementation, so using it gives undefined behavior. This should be just:
struct ListofHops {
int macAddrLtr;
int ttlValue;
};