When an object is placed inside a vector, the vector makes a copy of the object, this means that any changes to the object does not change the object inside the vector. How do i make so any changes to the object also change the object inside the vector.
#include <iostream>
#include <vector>
using namespace std;
class objectClass {
public:
int x = 0;
};
vector<int> my_vector;
int main()
{
objectClass testObject;
vector<objectClass> my_vector = {testObject};
testObject.x = 5;
cout << my_vector[0].x;
}
//outputs 0
You can do this in two way
change the object from the vector like :
#include <iostream>
#include <vector>
using namespace std;
class objectClass {
public:
int x = 0;
};
vector<int> my_vector;
int main()
{
objectClass testObject;
vector<objectClass> my_vector = {testObject};
my_vector[0].x = 5;
cout << my_vector[0].x;
}
or you can do this by using pointers or reference, use a vector of pointers, where the vector will have the collections of objects references and thus changing the object from outside the vector also will change from inside the vector.
#include <iostream>
#include <vector>
using namespace std;
class objectClass {
public:
int x = 0;
};
int main()
{
objectClass testObject;
vector<objectClass*> my_vector = {&testObject};
testObject.x = 5;
cout << my_vector[0]->x;
}
One way of doing it would be having a vector of pointers instead, where each pointer points to some instance of objectClass:
#include <iostream>
#include <vector>
using namespace std;
class objectClass {
public:
int x = 0;
};
int main()
{
objectClass testObject;
vector<objectClass*> my_vector = {&testObject};
testObject.x = 5;
cout << my_vector[0]->x;
}
Related
I am trying to print the first value from each vector shown below in the main function.
#include <iomanip>
#include <map>
using namespace std;
typedef unsigned int vect;
int main() {
std::vector<vect> p;
vector<vect> a = { 4,2,3,1 };
vector<vect> b = { 4,2,3,1 };
vector<vect> c = { 4,2,3,1 };
vector<vect> d = { 4,2,3,1 };
int i;
for (i=0; i<a.size(); i++)
cout << a[i];
}
Function first_preference() from my function.cpp shown below
#include "function.h"
#include <string>
#include <iostream>
using namespace std;
person test::first_preference() const {
const person& first = p.front();
return first; //current first pref
}
The function is declared in this header class
#ifndef FUNCTION_H
#define FUCNTION_H
#include <vector>
typedef unsigned int person;
typedef unsigned int vect;
std::vector<vect> p;
class test {
public:
person first_preference() const;
};
#endif
I want the function first_preference() to be called from main() where the function should print the first value of each vector, how would I go about this?
I want the function first_preference() to be called from main() where the function should print the first value of each vector
Some issues:
You have a global std::vector<vect> p in your header file (which is not a good idea to begin with) which is shadowed by std::vector<vect> p in main. What you put in the p in main will not be accessible from instances of test. Those instances only knows about the global p.
You don't #include "function.h" in main.cpp so you can't create test objects in main.
If you #include "function.h" in main.cpp there's no need to typedef unsigned int vect; since you did that in function.h already. It's not an error, but confusing and unnecessary.
The vector<vect> instances a, b, c and d have no connection with test or any of the ps whatsoever so what you put in those vectors can't possibly be printed by instances of test unless you pass them on to test somehow.
You declare vectors of vect but first_preference() returns a person by value. vect and person happen to be aliases of the same fundamental type, but it seems like there is something wrong with this interface.
In main.cpp you don't instantiate a test, you iterate over a and first_preference() is never called so there's no hope for it to be used.
Why is “using namespace std;” considered bad practice?
This question already has answers here:
Initialize base class with no default constructor in constructor of derived class
(3 answers)
Closed 3 years ago.
I'm learning oop in c++ and have the following error:
In constructor 'pflFr1::pflFr1()'
Error: No matching function for call to worldBuilder::worldBuilder()
The class worldbuilder is generating protected 2d arrays. The class pflfr1 is inherting from worldbuilder. But somehow, the constructor of worldbuilder has a problem.
My code:
main.cpp:
#include <iostream>
#include "pflfr1.h"
#include "worldbuilder.h"
#include <vector>
using namespace std;
int main()
{
srand(time(0));
int sl = 10;
worldBuilder w_obj(sl);
w_obj.buildPhyWelt();
w_obj.buildVegWelt();
pflFr1 objekt();
return 0;
}
worldbuilder.h:
#ifndef WORLDBUILDER_H
#define WORLDBUILDER_H
#include <vector>
using namespace std;
class worldBuilder
{
public:
worldBuilder(int sl_);
vector<vector<int> > buildPhyWelt();
vector<vector<int> > buildVegWelt();
protected:
vector<vector<int> > phyWelt;
vector<vector<int> > vegWelt;
int sl;
private:
};
#endif // WORLDBUILDER_H
worldbuilder.cpp:
#include <iostream>
#include "worldbuilder.h"
#include <vector>
using namespace std;
worldBuilder::worldBuilder(int sl_)
: sl(sl_)
{
}
vector<vector<int> > worldBuilder::buildPhyWelt()
{
phyWelt.resize(sl, vector<int>(sl));
// initialisiere physische Welt
// 1 = land, -1 ist meer
for(int i=0; i<sl; i++)
{
for(int j=0; j<sl; j++)
{
phyWelt[i][j] = 1;
}
}
}
vector<vector<int> > worldBuilder::buildVegWelt()
{
vegWelt.resize(sl, vector<int>(sl));
// initialisiere Nahrung
for(int i=0; i<sl; i++)
{
for(int j=0; j<sl; j++)
{
if(rand()%100<=2)
{
vegWelt[i][j] = 1;
}
else
{
vegWelt[i][j] = 0;
}
}
}
}
pflfr1.h:
#ifndef PFLFR1_H
#define PFLFR1_H
#include <vector>
#include "worldbuilder.h"
using namespace std;
class pflFr1: protected worldBuilder
{
public:
pflFr1();
protected:
private:
int y;
int x;
int hp;
};
#endif // PFLFR1_H
pflfr1.cpp:
#include <iostream>
#include <pflfr1.h>
#include <cstdlib>
#include <ctime>
using namespace std;
pflFr1::pflFr1()
: hp(10)
{
int initPosY = rand()%sl;
int initPosX = rand()%sl;
y = initPosY;
x = initPosX;
}
Your default constructor pflFr1::pflFr1() 's implementation is rewritten as
pflFr1::pflFr1()
: worldBuilder(), hp(10){
...
}
Because the pflFr1 class derives from worldBuilder, its constructor must be called as part of pflFr1 object creation. The default behaviour is to call the default constructor of the base - worldBuilder().
But this constructor does not exist. You only declared worldBuilder::worldBuilder(int sl_);. By doing so you disabled the automatic generation of default constructors for classes, which is done only if no user-defined constructor was declared.
Either you can write worldBuilder()=default; inside the class, which will leave s1 uninitialized. Or you can use delegating constructors - worldBuilder():worldBuilder(0){}.
How would I access my vector declared in aDie.h from aHisto.h, preferably not as a function? Once my vector is in my Histo.h I want to be able to modify it, change size, and just manipulate it however I choose. I am just having troubles with error messages in VS.
aDie.h
#define aDie_H
#pragma once
#include <vector>
#include <iostream>
class aDie {
public:
aDie();
void numRolls();
void getSeed();
void roll();
void myVector(); //just gives my vector values
void Print();
std::vector<int> myV; //declare my vector, it has values stored from void myVector();
private:
int i = 0;
int Rolls;
int dSeed;
int die1;
int die2;
int sum;
};
aHisto.h
#define aHistogram_H
#include "aDie.h"
class aHistogram : public aDie{
public:
//adds a pointer to my vector so I can access and modify it anywhere on this header
aHistogram(); //default const
void getVector(); //does stuff with vector
private:
int i = 0;
int min;
int max;
};
aHisto.cpp
#include "aHistogram.h"
#include "aDie.h"
#include <iostream>
#include <vector>
using namespace std;
aHistogram::aHistogram() { //default constructor
min = 0;
max = 0;
}
void aHistogram::getVector() {
//does stuff with vector here
}
Use the scope resolution operator because aHistogram inherits from aDie.
or you could use this because of the inheritance access level.
void aHistogram::getVector() {
aDie::myV->something();
//Or use this pointer
this->myV->something();
}
What I want is to access the data info of an managed shared memory object using a class named ShmObj with the raw pointer to the shared objects as private member, as code blocks below.
My problem is the main program segmentation fault. I guess the absolute raw pointer causes the problem. I tried to change the raw pointer to bi::offset_ptr but doesn't help. Any help is appreciated.
ShmObj.h
#include <string>
#include <boost/interprocess/managed_shared_memory.hpp>
namespace bi = boost::interprocess;
class ShmObj {
public:
ShmObj() {
bi::managed_shared_memory segment(bi::open_only, "shm");
pNum = segment.find<int>("Number").first;
}
int getNumber() {return *pNum;}
virtual ~ShmObj() {}
private:
int* pNum;
};
main.cpp
#include "ShmObj.h"
#include <iostream>
int main() {
ShmObj X;
std::cout << X.getNumber() << std::endl;
}
Your shared memory segment is destructed at the end of the constructor... Move it to a field to extend its lifetime:
#include <string>
#include <boost/interprocess/managed_shared_memory.hpp>
namespace bi = boost::interprocess;
class ShmObj {
public:
ShmObj()
: segment(bi::open_or_create, "shm", 32ul*1024),
pNum(0)
{
pNum = segment.find_or_construct<int>("Number")(0);
}
int getNumber() {
assert(pNum);
return *pNum;
}
virtual ~ShmObj() {}
private:
bi::managed_shared_memory segment;
int* pNum;
};
#include <iostream>
int main() {
ShmObj X;
std::cout << X.getNumber() << std::endl;
}
I have simple class in a header file: a.hh
#ifndef a_hh
#define a_hh
class a
{
public:
int i;
a()
{
i = 0;
}
};
#endif
Then i have a file:b.cc
#include <iostream>
#include "a.hh"
using namespace std;
int main(int argc, char** argv)
{
a obj;
obj.i = 10;
cout << obj.i << endl;
return 0;
}
>
Till this point everything is fine.
I compile the code and it compiles fine.
But as soon as i add a vector in the class:
#ifndef a_hh
#define a_hh
class a
{
public:
int i;
vector < int > x;
a()
{
i = 0;
}
};
#endif
I get a compilation error as below:
> CC b.cc
"a.hh", line 7: Error: A class template name was expected instead of vector.
1 Error(s) detected.
What is the problem with declaring a vector here as a member?
You need to #include <vector> and use the qualified name std::vector<int> x;:
#ifndef a_hh
#define a_hh
#include <vector>
class a{
public:
int i;
std::vector<int> x;
a() // or using initializer list: a() : i(0) {}
{
i=0;
}
};
#endif
Other points:
(as commented by EitanT) I removed the additional qualification a:: on the constructor
have a read of Why is "using namespace std" considered bad practice?
declaring a vector as a class member:
#include <iostream>
#include <vector>
using namespace std;
class class_object
{
public:
class_object() : vector_class_member() {};
void class_object::add_element(int a)
{
vector_class_member.push_back(a);
}
void class_object::get_element()
{
for(int x=0; x<vector_class_member.size(); x++)
{
cout<<vector_class_member[x]<<" \n";
};
cout<<" \n";
}
private:
vector<int> vector_class_member;
vector<int>::iterator Iter;
};
int main()
{
class_object class_object_instance;
class_object_instance.add_element(3);
class_object_instance.add_element(6);
class_object_instance.add_element(9);
class_object_instance.get_element();
return 0;
}
1.You need to #include <vector> and using namespace std, then a.hh just like below:
#ifndef a_hh
#define a_hh
#include <vector>
using namespace std;
class a
{
public:
int i;
vector <int> x;
a()
{
i = 0;
}
};
#endif
2. If you don't want to only use std namespace in all your code, you can specified the namespace before type, just like std::vector<int> x;