Im wondering what I've done wrong in my code that is throwing the error.
(Note that due to external restrictions the use of -> is not allowed and brackets[] are not allowed anywhere except during array declaration)
Here are my two structs:
struct RentalCar
{
char make[10];
char model[10];
int year;
float price;
bool available;
};
struct Agency
{
char name[10];
int zipcode;
RentalCar inventory[5];
RentalCar* myCar = inventory;
};
Later in my code I have the following
Agency myAgencies[3];
Agency* myPointer = myAgencies;
I later try to reference the following when I get my error
inFile>>(*myPointer).(*myCar).make;
Due to external conditions, I'm not allowed to use the following fix:
inFile>>(*myPointer).inventory[0].make;
Any help is appreciated!
Your pointer dereferencing syntax isn't quite right. Perhaps the easiest way to fix this is by rewriting the
(*myPointer).(*myCar).make
using the arrow operator:
myPointer->myCar->make
If you cannot use -> then what what you can use is drop using the pointers and use the arrays themselves.
inFile>>(*myPointer).(*myCar).make;
Becomes
inFile >> myAgencies[0].inventory[0].make;
Related
I predict char value type could cast pointer type only.
Am I right?
And what is those difference between Struct1 and Struct2?
Moreover, Somebody says it could be an error.
struct UdpReceive ReceiverA;
UdpReceive ReceiverB; // <= It's supposed to be an Error.
But I couldn't make this happen in C++, then compile succeed.
Does this error happen only in C?
Thanks.
int main()
{
//It will be assumed containd Byte data of UDP.
unsigned char ReceivedData[128];
//Struct pt1
struct UdpReceive {
char name[20];
int age;
double hegiht;
};
UdpReceive ReceiveBuffer;
UdpReceive* pReceiveBuffer;
//Struct pt2
typedef struct{
char name[20];
int age;
double hegiht;
}V2SUdpReceive, *V2SUdpReceive;
V2SUdpReceive V2ReceiveBuffer;
V2SUdpReceive* pV2ReceiveBuffer;
pReceiveBuffer = (UdpReceive*)ReceivedData;
ReceiveBuffer = (UdpReceive)ReceivedData;// <= it's not pointer type that is reason why Error.
pV2ReceiveBuffer = (V2SUdpReceive*)ReceivedData;
V2ReceiveBuffer = (V2SUdpReceive)ReceivedData;// <= it's not pointer type that is reason why Error.
}
In C you could cast a char pointer to a struct.
In C++ you can do it as well, but you should not do it. Although it may work. But C++ gives you no guarantee about how the data of the struct is represented internally.
Your problem is called serializing / deserializing. You have struct and you want to send its contents over some connection to a other party. But the other party may have a different internal memory representation of such a struct. And the correct reception of the data would fail.
If you want to do it for yourself in none productive code, try the casting and, if it works use it.
Maybe better: Create some functions for serializing / deserializing, maybe overwrite the inserter >> and extractor operator <<. Or, use a common serilization framwork, which you can find on the net.
So I've made a class, one of it's function returns a struct vector, like so:
vector<highscore::players> highscore::returnL(){
load();
return list;
}
So list is basically,
struct players {
string name;
int score;
};
vectors<players> list;
In my source cpp, I tried to capture this vector, so I made another struct and struct vector.
Source.cpp:
struct players1 {
string name;
int score;
};
vector<players1> highscorelist;
Then I tried to
highscore high; //class' name is highscore
highscorelist = high.returnL();
But I get the error message:
No operator "=" matches these operands
" operand types are std::vector<players1, std::allocator<players1>> = std::vector<highscore::players, std::allocator<highscore::players>> "
Is it not possible to do it this way?
I honestly don't know what to search for so this might have been answered before, apologize if that's the case.
You could use reinterpret_cast, but that's not a good solution. Why don't you use highscore::player?
std::vector<highscore::player> highscoreList;
highscoreList = high.returnL(); // ok
highscore::player and player1 are different types, even though they have the same variables and probably even the same memory layout. You cannot just interchange types like that. Also, if you change one of those types, you have to change the other, which is just a maintenance nightmare if it were possible.
If you can, you could also use auto:
auto highscoreList = high.returnL();
My first post here, thanks in advance.
Im new to C++ and am struggling with this mistake.
typedef struct {
int x;
int z;
char ref[20]; // or of other adequate type
DATE date;
bool put;
int hasPiece = false;
} TRequest;
And when i build it displays the error in the title
"<unnamed-tag>::hasPiece' : an in-class initializer is not allowed for a member of an anonymous union in non-class scope.
Can you please help me?? Thanks so much
In c++98 you're not allowed to give default values to members of a struct, although you may be able to in later standards. Use this approach instead:
struct TRequest
{
TRequest()
: hasPiece(0)
{
// Nothing to do here
}
int x;
int z;
char ref[20]; // or of other adequate type
DATE date;
bool put;
int hasPiece;
};
Note that you should be initalising all of the other members too, not just hasPiece. I've also fixed some other bits, you didn't need the typedef.
EDIT: Just noticed hasPiece is an int, why are you initialising it to false? It should be a bool, or initialised to 0. I've changed my answer to initialise it to 0.
Im trying to learn dynamic arrays in C++. For integer, dynamic arrays are like that:
int main()
{
int x;
cin >> x;
int *dynamic = new int[x];
//some codes
delete [] dynamic;
return 0;
}
How can i create dynamic struct array? I tried this code and i failed.
struct Phone{
char name[30];
char number[20];
}
int main(){
int x;
cin >> x;
Phone *record;
Phone *record = new Phone[x];// Code fails here
}
Im so confused in dynamic arrays. Please help me. Thanks.
There is no difference in syntax between allocating an int and allocating a struct.
Your syntax is correct. You're just defining the record pointer twice. Remove the first definition and you're all set (oh, and missing a semicolon after the struct{} declaration).
Note that modern C++ would probably prefer using an existing STL container (vector<Phone> or similar) instead of manually calling new and delete[]. I assume this is for learning, not for production code.
I would also suggest to rather use an std::vector. It's going to save you a lot of issues and memory bugs. Just do:
struct structName
{
...
}
std::vector<structName> structVector;
Then to get the values onto the vector do a
structVector.push_back(structName{});
I have got a very strange problem.
Here is my code:
Header File
namespace test
{
class LoadConfigData
{
struct control_params
{
char active[];
char suspended[];
char erased[];
}*ctrl_params;
bool loadConfig();
};
};
Main.cpp
using namespace std;
using namespace test;
namespace test
{
extern LoadConfigData *loadConfigDataobj;
LoadConfigData *loadConfigDataobj = new LoadConfigData;
};
int main()
{
loadConfigDataobj->loadConfig();
cout <<loadConfigDataobj->ctrl_params->active_status_code_v<<endl;
cout <<loadConfigDataobj->ctrl_params->suspended_status_code_v<<endl;
cout <<loadConfigDataobj->ctrl_params->erase_status_code_v<<endl;
return 0;
}
bool LoadConfigData::loadConfig()
{
std::string a = "AC";
std::string b = "SP";
std::string c = "ER";
LoadConfigData::ctrl_params = new LoadConfigData::control_params;
sprintf(loadConfigDataobj->ctrl_params->active,"%s",a.c_str());
sprintf(loadConfigDataobj->ctrl_params->suspended,"%s",b.c_str());
sprintf(loadConfigDataobj->ctrl_params->erased,"%s",c.c_str());
return true;
}
Output:
ER
ER
ER
Which means that it's printing the last copied string for each struct member.
What is wrong in my code.
The problem is that you don't give the character arrays a size:
struct control_params
{
char active[];
char suspended[];
char erased[];
}*ctrl_params;
I'm rather surprised that this compiles; my understanding was that an unsized array was an incomplete type, and so couldn't be a non-static class member. However, my compiler at least (and presumably yours) treats these as arrays of size zero, all at the same location in memory. Therefore, each time you write to one it overwrites whatever you wrote to the others. (Of course, the behaviour is undefined since it writes outside the array bounds).
The simplest solution is to use std::string to represent the strings. That will manage its size automatically, and you can simply write active = a rather than messing around with sprintf (or, slightly more sensibly, strncpy) and hoping you don't get a buffer overrun.
The problem is your usage of sprintf. sprintf will not allocate any memory for you, active, suspended and erased are pointing to strange and undefined addresses. You are invoking undefined behaviour.
Without adding all the normally necessary detail, use std::string and it's operator overloads instead. For simple parsing, use streams. For now, totally avoid any C-string-function, and instead use C++ solely.