I could not declare an array of strings in my class. Below my class definition:
class myclass{
public:
int ima,imb,imc;
string luci_semaf[2]={"Rosso","Giallo","Verde"};
};
and my main file
#include <iostream>
#include <fstream>
#include "string.h"
#include <string>
using namespace std;
#include "mylib.h"
int main() {
return 0;
}
Why do I get the following warnings / error?
You have two problems: The first is that you can't initialize the array inline like that, you have to use a constructor initializer list. The second problem is that you attempt to initialize an array of two elements with three elements.
To initialize it do e.g.
class myclass{
public:
int ima,imb,imc;
std::array<std::string, 3> luci_semaf;
// Without C++11 support needed for `std::array`, use
// std::string luci_semaf[3];
// If the size might change during runtime use `std::vector` instead
myclass()
: ima(0), imb(0), imc(0), luci_semaf{{"Rosso","Giallo","Verde"}}
{}
};
You can not initialize data member.
You can write like this:
class myclass{
public:
myclass() {
luci_semaf[0] = "Rosso";
luci_semaf[1] = "Giallo";
luci_semaf[2] = "Verde";
}
private:
int ima,imb,imc;
string luci_semaf[3];
};
You can assign the values of the array in the Сonstructor
You're declaring an array of size 2 but providing 3 strings!
Try storing the elements in vector of strings, in c++ vectors are used more often.
class myclass{
public:
int ima,imb,imc;
std::vector<std::string> strings;
myclass() {
strings.push_back("blabla");
}
};
Related
i have a class which has several members of char arrays, and i want to initialize this class with an array of strings which are the values of the char arrays.
class Product {
public:
char productId[20];
char productName[50];
char price[9];
char stock[9];
Product(vector<string> v) : productId(v[0]), productName(v[1]), price(v[2]), stock(v[3]) { }
};
with this code i get an error that say no suitable conversion function from "str::string" to "char[20]" exist
The code at the bottom will work. But is this a good idea? Probably not. You are better of just storing std::string in your Product type directly:
#include <cassert>
#include <iostream>
#include <vector>
#include <string.h>
class Product {
public:
std::string productId;
...
Product(std::vector<std::string> v) : productId{std::move(v[0])} {}
};
There is a problem with this code though; where do you check the vector has all required elements? Better to make an interface that specifies the four strings a Product is made up of separately:
class Product {
public:
std::string productId;
...
Product(std::string pid, ...) : productId{std::move(pid)}, ... {}
};
But in case you insist on a C/C++ amalgamation;
#include <cassert>
#include <vector>
#include <string.h> // strcpy
class Product {
public:
char productId[20];
char productName[50];
char price[9];
char stock[9];
Product(const std::vector<std::string> &v) {
assert(!v.empty()); // really; v.size() == 4
::strncpy(productId, v[0].c_str(), 19);
productId[19] = '\0';
// ...etc.
}
};
So the problems are:
Class Stan must have a constructor that enables it to recieve and store unlimited nubmer of elements and it has to be implemented using templates. Also, demonstrate it in the main function
Class Stan must have a constructor that enables it to recieve and store unlimited nubmer of elements and it has to be implemented using initializer lists. Also, demonstrate in main function
Place class Stan into the namespace Zgrada
Lets assume that there are 2 types of Stans - Apa & Gar. Within the main function it is necessary to demonstrate polimorphism by implementing getVrsta(); which returns one of the 2 types
I have no clue how to do more than this.
Thanks for the help!
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
#include <iterator>
using namespace std;
int Element::brV = 0;
class Element {
public:
string naziv;
double obujam;
static int brV;
Element(string n, double o) : naziv(n), obujam(o) {
if (o > 3.) brV++;
}
int getVelikiElementi(){
return brV;
}
void virtual GetVrsta(){
cout << "Vrsta Stana: ";
}
};
template <class T> //I think i got the templates_init right
class Stan : public Element {
public:
vector<Element> Elementi;
template<class...T>
Stan(T...arg) : Elementi({arg...}){}
Stan(initializer_list<Elementi>) : Element (){} // But this most certainly not
void GetVrsta(){
}
};
int main () {
Element X("a", 3.);
Element Y("b", 2.);
Element Z("c", 1.);
vector <Element*> E={X,Y,Z}; // initilizer_lista_const_call
return 0;
}```
I have a problem with vector declaration and initialization in a
class constructor. I have a Station.h and Station.cpp files of a class and I recall it in main :
Station.h
#ifndef STATION_H
#define STATION_H
#include <vector>
class Station
{
public:
int num_bin;
int num_staz;
vector<int> binari; //here already gives me error! Vector does not name a type
Station(int num_staz, int num_bin);
virtual ~Station();
Station(const Station& other);
protected:
private:
};
Then I want to initialize the vector in the constructor of .cpp like that:
Station.cpp
#include "Station.h"
using namespace std;
Station::Station(int num_staz, int num_bin)
{
this->num_bin = num_bin;
this->num_staz = num_staz;
this->binari(num_bin); //here I want to create a vector of num_bin size
}
and then call it in main like that:
main.cpp
#include <iostream>
#include "Station.h"
using namespace std;
int main()
{
Station staz1(2,3);
staz1.binari.push_back(300); // error! class Station has no member binari
staz1.binari.push_back(250);
staz1.binari.push_back(150);
return 0;
}
Where am I making a mistake?
this->binari(num_bin); //here I want to create a vector of num_bin size
The function you need to use is std::vector::resize().
this->binari.resize(num_bin);
It will be better to initialize the object with the appropriate size as:
Station::Station(int num_staz, int num_bin) : num_bin(num_bin),
num_staz(num_staz),
binari(num_bin)
{
}
this->binari(num_bin); This doesn't work because it is not an initialization that is why it doesn't work.
To make this work use it in in-class initialization list:
Station::Station(int num_staz, int num_bin) :
num_bin(num_bin),
num_staz(num_staz),
binari(num_bin)
{
}
I'm trying to create a vector with a specific size, of 255 (max)..
It doesnt work for me, like I see in examples over the internet...
I'm using Microsoft Visual C++ 2012...
I have the current code :
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
const int MAX = 255;
class test
{
vector <string> Name(MAX);
};
int main()
{
system("PAUSE");
}
It gives me 2 errors :
Error 1 error C2061: syntax error : identifier 'MAX'
2 IntelliSense: variable "MAX" is not a type name
Thanks for your help!
That's not valid syntax for a class declaration. Try:
class test
{
vector <string> Name;
test() : Name(MAX) {}
};
You can write vector <string> Name(MAX); when you create a variable (in your case, you're declaring a member). For example:
int main()
{
vector <string> Name(MAX);
}
would be perfectly valid.
You can't pass arguments to the std::vector constructor int the class declaration. You should put that in the constructor for your class, like this, which utilizes does it via an initializer list:
class test
{
std::vector<std::string> Name;
public:
test():
Name(MAX)
{
}
};
You cannot initialize a data member inside the class declaration like this. Use the member initialization list in your constructor of the class to initialize vector<string> Name.
test::test
:Name(MAX)
{
//
}
Your main would be like this.
test t1 ;
It would automatically call the constructor and all the fields of t1 would be created, including vector<string> Name.
I have read other similar posts but I just don't understand what I've done wrong. I think my declaration of the vectors is correct. I even tried to declare without size but even that isn't working.What is wrong??
My code is:
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <cmath>
using namespace std;
vector<string> v2(5, "null");
vector< vector<string> > v2d2(20,v2);
class Attribute //attribute and entropy calculation
{
vector<string> name(5); //error in these 2 lines
vector<int> val(5,0);
public:
Attribute(){}
int total,T,F;
};
int main()
{
Attribute attributes;
return 0;
}
You cannot do this:
vector<string> name(5); //error in these 2 lines
vector<int> val(5,0);
in a class outside of a method.
You can initialize the data members at the point of declaration, but not with () brackets:
class Foo {
vector<string> name = vector<string>(5);
vector<int> val{vector<int>(5,0)};
};
Before C++11, you need to declare them first, then initialize them e.g in a contructor
class Foo {
vector<string> name;
vector<int> val;
public:
Foo() : name(5), val(5,0) {}
};
Initializations with (...) in the class body is not allowed. Use {..} or = .... Unfortunately since the respective constructor is explicit and vector has an initializer list constructor, you need a functional cast to call the wanted constructor
vector<string> name = decltype(name)(5);
vector<int> val = decltype(val)(5,0);
As an alternative you can use constructor initializer lists
Attribute():name(5), val(5, 0) {}
Since your compiler probably doesn't support all of C++11 yet, which supports similar syntax, you're getting these errors because you have to initialize your class members in constructors:
Attribute() : name(5),val(5,0) {}