Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
So I'm trying to write a little personal Planner App, and want store information in a class Tasks,
nothing to fancy yet but strcpy does not recognize the string c_task;
compiler say c:tastk is std:string Tasks::c_task so I understand that it is not the right argument for strcpy. So the question is how to make strcpy accept that string ?
class Tasks {
public:
string c_task = "";
int date;
int category;
int priority;
void newTask(string n_task);
};
void Tasks::newTask(string n_task)
{
strcpy_s(c_task, n_task);
}
Use std::string related functions. strcpy_s acts upon char * not on std::string.
class Tasks {
public:
std::string c_task = "";
int date;
int category;
int priority;
void newTask(std::string n_task);
};
void Tasks::newTask(std::string n_task)
{
c_task = std::move(n_task);
}
Strcpy is a standard library function for C language.You should use copy constructor of string.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I am very new to C++ and I am trying to initialize an object called GameObject, in a class called Room, which holds a gameObjects array. The constructor of the GameObject class takes pointers as the parameters to initialize the fields. But I keep getting the error saying that there is "No matching constructor for initialization of GameObject. Could someone tell me what is my mistake here?
Sorry if this question is formatted badly, I am not used to asking C++ questions with multiple header files and source files. But please also correct me on this.
GameObject
GameObject::GameObject(string* _name, string* _description, char* _keyWord):
name(_name), description(_description), keyWord(_keyWord){
}
Room
//error!, "No matching constructor for initialization..."
gameObjects[0] = new GameObject("knife", "a knife", 'k');
gameObjects[1] = new GameObject("sword", "a sword", 's');
};
Well, you in the GameObject constuctor:
GameObject::GameObject(string* _name, string* _description, char* _keyWord):
name(_name), description(_description), keyWord(_keyWord){}
You are accepting the two strings and a char by pointer. So maybe you meant to accept them by reference instead like this:
GameObject::GameObject(string& _name, string& _description, char _keyWord):
name(_name), description(_description), keyWord(_keyWord){}
Which works fine.
For instance take this program:
#include<iostream>
#include<string>
struct my_obj
{
my_obj(std::string s, std::string s_, char c)
: s__(s), s___(s_), char_(c) {}
std::string s__{};
std::string s___{};
char char_{};
};
struct my_obj_2
{
my_obj(std::string* s, std::string* s_, char* c)
: s__(s), s___(s_), char_(c) {}
std::string s__{};
std::string s___{};
char char_{};
};
int main()
{
my_obj my_obj_1("Hello", "Goodbye", 'C'); // works fine
my_obj_2 m_my_obj_2("Hello", "Goodbye", 'c'); // doesn't work fine
}
(1): This works fine because it finds a matching constructor:
std::string -> std::string
(2): Whereas this doesnt work fine because it is this conversion:
std::string -> std::string*
So in conclusion just remove the pointers on the constructor and if you wanted to you could pass then by & or const & like this
GameObject::GameObject(string& _name, string& _description, char _keyWord):
name(_name), description(_description), keyWord(_keyWord){}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I dont exactly get the use of a constructor
For example:
class car
{
public:
string name;
long int price;
int mileage
bool ownedByPlayer;
};
Here, why will I use a constructor like this,
class car
{
public:
string name;
long int price;
int mileage
bool ownedByPlayer;
car()
{
ownedByPlayer = false;
}
};
if I can simply define the variable ownedByPlayer where I have declared it. Like: bool ownedByPlayer = false; ?
In your case there is no need to write the constructor. Actually it is recommended to not write a constructor that does nothing but initialize members with default values. Though the correct way would be to use the member initializer list:
car() : ownedByPlayer(false) {}
And since C++11 you can use a default member initializer instead:
class car {
// ...
bool ownedByPlayer = false;
// ...
};
Sometimes constructors actually need to do something. Consider a car registers itself somewhere after being constructed, then your class could look like this:
class car
{
public:
string name;
long int price;
int mileage;
car(string name,long int price, int mileage) : name(name),price(price),mileage(mileage) {
CarRegistrationFacility::register_car(*this);
}
};
Actually I found it difficult to make up an example for a constructor that does more than initializing members, because thats what constructors do. However, when your constructor does more than that, then that "more" happens in the body of the constructor.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have come across a rather unusual issue in my code. A struct needs to be able to access instances of itself.
Relavent portion of code:
struct crtr {
char f;
void foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
};
vector<crtr> creatures[10];
Of course, this is nowhere close to working - crtr.foo() requires creatures, while creatures requires crtr. Is there some way to initialize creatures before crtr, perhaps changing the vectors' data type? (preferably with minimal pointers, if possible)
I must be missing something, what's wrong with this?
struct crtr {
char f;
void foo();
};
vector<crtr> creatures[10];
void crtr::foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
Also crtr::foo can be inline if that's required.
Use forward declaration of the struct:
struct crtr;
vector<crtr> creatures[10];
struct crtr {
char f;
void foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
};
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class AAA {
private:
vector<double> v;
public:
AAA(int n);
void AAAprint();
void testfn(AAA &aaa);
};
AAA::AAA(int n){
v=vector<double>(n,0);
}
void AAA::AAAprint(){
for (int i=0;(unsigned int) i<v.size();i++)
{
std::cout<<v[i]<<" ";
}
std::cout<<std::endl;
}
void AAA::testfn(AAA &aaa)
{
aaa.v[0]=5;
}
int main(){
AAA aaa(25);
aaa.AAAprint();
AAA bbb(25);
bbb.testfn(aaa);
aaa.AAAprint();
}
In main i first construct aaa then print out its value. Next aaa is changed after calling bbb.testfn(aaa). In void AAA::testfn(), I could directly access to aaa.v and the value of aaa.v[0] is indeed changed outside of void AAA::testfn(). Since I printout aaa.v in the end of main function it shows so. What is the reason for this?
Privcy is a class restriction not an instance restriction. All instances of a class have full access to private members of all other instances.
There are quirks about inheritance and protected and distinct instances, but that is another question.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a couple of questions I wanted to check with SO for my Data Structures in C++ course. They deal with the following class and multidimensional array:
class Order
{
public:
Order();
void addItem(string name, double price);
private:
static const int MAX_ITEMS = 10;
string itemNames[MAX_ITEMS];
int numItems; // # of items actually stored
double totalPrice;
};
const int TABLES = 10;
const int SEATS = 4;
Order diningRoom[TABLES][SEATS];
Q1: How many copies of MAX_ITEMS does the array diningRoom contain?
This is 40 right? There is one copy for each element in the array, 10*4.
Q2: Member function addItem should have been instead declared how?
A.) void addItem(const string &name, double price);
B.) void addItem(string &name, double price);
C.) void addItem(string name, double price) const;
D.) void addItem(string name[], double price);
A? Pass by const reference? This one I'm not too sure of.
No. There is one instance of MAX_ITEMS since it is a static member.
Yes. Const reference is the way to go.