I try to print values from my object class, but I am unable to properly access the information stored at the pointer. Below I have defined a simple struct.
When compiled, I get an error:
no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::vector<int>')
void PrintNode(Node *node) { cout << node->key << endl; }
struct Node
{
vector<int> key;
int parent;
Node(vector<int> x, int y){ key = x; parent = y; }
void PrintNode(Node* node) { cout << node->key << endl; }
};
I call my PrintNode in my BFS function:
void BFS( vector<int> permutation, int n ) {
vector<Node*>Pointers;
queue<Node*> Queue;
Node* start = new Node(permutation, -1);
Node::PrintNode( start );
Pointers.push_back( start );
}
I don't understand why I am unable to cout the integer vector stored in .key of the node object. I believe that I am dereferencing the pointer correctly with node->key.
The standard library doesn't support direct iostreams output of a vector. But you can easily define such an operation. Just do it with a loop.
std::cout cannot handle raw vectors, you must convert it to an array which is can process first. You can do this using vector's .data() method
Example:
void PrintNode(Node* node) { cout << node->key.data() << endl; }
I try to print values from my object class, but I am unable to properly access the information stored at the pointer. Below I have defined a simple struct.
The simple answer to this question is the fact that std::vector<type, allocator> does not have an overload for the std::ostream::<< operator. Hence when you try to print out the entire vector of keys, it won't work the way you expect it to. I have seen several answers on other posts which suggest overloading the << operator for std::vector but unless you know what you are doing I would avoid doing this for several reasons, one of them being global namespace pollution and the second being incorrect handling of the overloading itself.
Also, please stop doing using namespace std;. It will not help you in any way and just make things worse in the most unexpected ways.
Here are some fixes which may help.
Part 1 - Node struct
struct Node : public std::enable_shared_from_this<Node>
{
std::vector<int> keys;
int parent;
Node(vector<int> x, int y) : keys(x), parent(y){}
Node(const Node& rhs): keys(rhs.keys), parent(rhs.parent) {}
Node(Node&& rhs) noexcept: keys(std::move(rhs.keys)), parent(rhs.parent){}
void PrintNode()
{
for (auto& key : node->keys)
cout << key << "\n";
}
};
Part 2 BFS Code
void BFS(std::vector<int>& permutation, int n )
{
/* I don't see the real value in creating pointers for your case. You can easily live with an instance of the class Node. This also gives you scoped initialization as the pointers vector goes out of scope, your nodes will get deallocated too. at least in the context, you have posted above, that seems desirable.
However, if you insist on creating pointers, you can use smart pointers.
*/
std::vector<std::shared_ptr<Node>> pointers;
std::queue<std::shared_ptr<Node>> queue; // not used??
auto start = std::make_shared<Node>(permutation, -1); // make a shared pointer
/* PrintNode in your code is an instance level function. Invoke it using the scope resolution operators . or ->. If you insist on doing it your way, then declare the function static. However, that has its own quirks and you need to understand static functions before you do this. */
start->PrintNode();
pointers.push_back(std::move(start)); // add your pointer to the vector.
}
That said the code excerpt you have posted makes little sense. I have just provided fixes for the parts you have provided. Does not guarantee that it will work in the larger context you may have at hand.
Related
this is a simple program for using a stack. it basically does all the stack operations, when I run it the programming is not working for some reason and I can't figure out the reason why. also, is there any way I can improve this current code?
this is the program code here
//
#include <iostream>
using namespace std;
struct stack {
int top = -1;
int size;
int* p;
} *stack;
struct stack* createStack(int size)
{
struct stack st;
st.p = new int[size];
st.size = size;
struct stack* stackPointer = &st;
return stackPointer;
}
void push(struct stack* st, int el) {
if (st->top == (st->size) - 1) {
cout << "this operation cannot be done as the size is full :(" << endl;
}
else
{
st->top = (st->top)++;
st->p[st->top] = el;
}
}
void pop(struct stack* st) {
if (st->top == -1)
cout << "stack is already empty" << endl;
else
st->p[st->top] = NULL;
}
void displayStack(struct stack* st) {
for (int i = 0; i <= st->top; i++) {
cout << st->p[i] << endl;
}
}
int main()
{
struct stack* st = createStack(5);
push(st, 1);
push(st, 2);
push(st, 3);
push(st, 4);
displayStack(st);
pop(st);
displayStack(st);
}
There are a few problems with your code. First, as others have said, you're just doing C code without cout. Instead, you might want stack to look like this:
struct stack {
stack(int sz);
void push(int value);
int pop();
int top = -1;
int size = 0;
int * p = nullptr;
};
std::ostream & operator<<(stack &stack) {
for (int i = 0; i <= stack->top; i++) {
cout << stack->p[i] << endl;
}
}
stack::stack(int sz)
: size(sz), p(new int[size])
{
}
void push(int value) {
if (top == (size) - 1) {
cout << "this operation cannot be done as the size is full :(" << endl;
}
else {
p[++top] = el;
}
}
int pop() {
return top < 0 ? INT_MIN : p[top--];
}
int main() {
stack st(5);
st.push(1);
st.push(2);
st.push(3);
st.push(4);
cout << "First dump: " << st << endl;
st.pop();
cout << "Second dump: " << st << endl;
}
At first: You tagged C++, but your code is C – apart from not providing void to main, outputting to std::cout and using namespace std – the latter you shouldn't do anyway!
createStack function should be a constructor instead, push and pop should be member functions and you should prevent access to the internal data by making it private. Typically, one would rather use a class than a struct (structs usually are used for POD types). That would look like:
class Stack
{
// default accessibility for class is private, so need to switch to public first
public:
Stack(size_t capacity)
: m_capacity(capacity), m_size(0), m_data(new int[capacity])
{ }
bool push(int value);
bool pop();
int top();
// now private section:
private:
size_t m_capacity;
size_t m_size;
//int* m_data;
// use of a smart pointer avoids necessity to care for memory management manually:
std::unique_ptr<int[]> m_data;
};
Sure, that looks pretty different now. But that's the C++ way. If you don't get along with you might want to peek in a good C++ book.
Some additional changes:
I renamed size to capacity, and top to size. Correct data type for specifying object or array sizes is std::size_t (or just size_t), you need to #include <cstddef> for. Note, though, that this type is unsigned (negative sizes are meaningless anyway).
Old top/new size has different semantics, not indexing the last element, but holding the number of elements – or index to one past the last element. This is rather typical semantics in C++ – and actually C as well.
The m_ prefix signals the variables being members of a class, it helps distinguishing class members from local variables. Such a convention is not uncommon, but no necessity. Decide you yourself if you want to follow or not...
I added a top function returning the last element on the stack. Note that the data and size members are private, so they cannot be accessed from outside the class, thus a user couldn't retrieve the top element without the function.
I changed the return types from void to bool – it is a pretty questionable idea to do any output from within a general purpose class. Users of it might want to provide different output, e.g. in another language, and you now are spoiling their programme. In other words: You limit reusability. So let's just return a success indicator and leave the output to the user (you personally would do so within main then).
Of course implementation needs to be changed a little bit, too. You might add the function bodies directly within the class definition (drop the semicolon then), usually you define the class in a header (stack.h or stack.hpp), but the member functions in a separate source file (stack.cpp). The latter would then contain:
#include "stack.h" // should always be the very first include:
// implicit check, if the header is self-contained, i.e.
// includes all headers it needs for the class definition
bool stack::push(int value)
// ^^ scope resolution: identifies the push function of class stack
{
if(m_size == m_capacity)
{
return false;
}
m_data[m_size++] = value;
return true;
}
bool stack::pop()
{
if(m_size == 0)
{
return false;
}
--m_size;
// you don't need to assign another value like 0
// it would be overwritten with next push anyway
//
// note that NULL would have been wrong anyway, that's for pointers!
// apart from, you should prefer C++ KEYWORDS over obsolete C MACROS,
// i.e. prefer nullptr over NULL
//
// note, too, that you did NOT reduce top on popping in your version
// should have caused outputting 1234 twice in your test code instead
// of 1234 and 123 – which I assume you meant by 'not working'
// – please get used to more precise error descriptions, by the way!
return true;
}
int top()
{
return m_data[m_size - 1];
}
Well, top is a pretty basic implementation, it relies on undefined behaviour if the stack is empty (i.e. it is the responsibility of the user to check size first!). Alternatively you could:
check the size yourself and throw an exception if the stack is empty
change the return type to bool and have a reference argument to provide the top value to (bool top(int& value);) – as being a reference, you indeed can do assignments to.
main would contain code like this one:
Stack s;
s.push(1);
// ^ call class member function; for pointers use ->
s.push(2);
std::cout << s.top();
s.pop();
Finally outputting the entire stack:
Have you noticed that you can write std::cout << 7 << someVariable << std::endl;? What if you could do the same with your stack?
No problem:
class Stack
{
public:
// see above
private:
// need to declare a FRIEND so that the function/operator has access to
// the private class members
friend std::ostream& operator<<(std::ostream& s, Stack const& s);
// private members, see above
};
std::ostream& operator<<(std::ostream& s, Stack const& s)
{
// now do the output to s just as the original displayStack did to std::cout
// but consider the changed semantics of top/size
return s;
}
That's it:
Stack s;
std::cout << s << std::endl; // now fine
That's called operator overloading.
EDIT: Considering paddy's comment to the question (didn't notice myself):
Main reason for your original programme failing was returning a local variable from the stack. That variable runs out of scope on leaving the function, though, so accessing it is actually undefined behaviour. What then happens technically is that the stack's contents (top, size and the pointer) likely get overwritten by next function call that reuses the stack. A problem gone away with the new class + constructor approach proposed above...
I have a task to create an object Stos which would feature a heap of objects Obiekt, to which I could add things as I please.
In order to make the program better support dynamic arrays I decided to use a Vector.
The whole implementation seems to run perfectly, the returned value is completely off.
Here is an example with code:
class Obiekt {
private:
int id;
public:
Obiekt::Obiekt(int i) {
id = i;
}
void Obiekt::display() {
cout << "This object has id of: " << id << endl;
}
};
class Stos {
private:
vector < Obiekt* > stos;
public:
Stos::Stos(Obiekt n) {
add(n);
}
void Stos::add(Obiekt n) {
stos.push_back(&n);
}
void Stos::display() {
cout << endl << "===HEAP DISPLAY===" << endl;
for (int i = 0; i < stos.size(); i++) {
stos[i]->display();
}
}
};
void Zad1()
{
Obiekt obj1(5);
Obiekt obj2(23);
Stos s1(obj1);
s1.add(obj2);
s1.display();
getchar();
}
And the outcome being:
===HEAP DISPLAY===
This object has id of: -858993460
This object has id of:9805925
I'm not a cpp expert, and believe the issue is related to the stos.push_back(&n) portion, but I can't catch the moment the id gets so distorted.
It's probably a noob question, so sorry for that on start.
Any help would be amazing.
The issue with your code as O'Neil correctly explained is that you're adding the pointer to a copy of the Obiekt object. So basically, you create your object in main, and pass it to the constructor and the .add function in Stos. You then add the pointer to the vector. When the function finishes, the copy that was passed is destroyed and the pointer in your vector is dangling.
There are two ways to fix this:
1 Pass by reference
This is very simple, basically you just add an ampersand to your function parameters. For instance:
void Stos::add(Obiekt &n) {
stos.push_back(&n);
}
This will ensure that the object isn't destroyed at the end of the function
2 Don't use pointers
Another way of getting your problem to work is to avoid using pointers at all. Your vector will actually copy the contents of the Obiekt object into it. For example:
vector < Obiekt > stos; // notice how we define it without the pointer type
...
void Stos::add(Obiekt n) {
stos.push_back(n); // Creates copy which will then contain the correct value
}
The parameters Obiekt n in
Stos::Stos(Obiekt n) {
add(n);
}
void Stos::add(Obiekt n) {
stos.push_back(&n);
}
are temporary copies destroyed immediatly after each call.
You have to use a reference Obiekt & n instead, or better: by pointer Obiekt * n.
I'm reluctant to assert that the objects exist at the time display is called.
Problem(s)
According to GCC's implementation they don't.
They fall out of scope and are immediately destructed. Give "Obiekt" a non-trivial destructor and this behavior becomes obvious:
~Obiekt(){std::cout << "Bye from: " << it << std::endl;}
Secondarily, note that you shouldn't specify the class membership for functions defined within the class itself (no class_name::function_name(parameters), just function_name(parameters) )
Possible Fix
You (might) want to changing "Stos" such that:
Stos(Obiekt &n) {add(n);}
void add(Obiekt &n) {stos.push_back(&n);}
This question already has answers here:
problem sorting using member function as comparator
(9 answers)
Closed 6 years ago.
I have been banging my head against this and google for a while now so I hope it is alright to ask. I am still learning C++ and enjoying it but I don't full understand everything yet and am really just now digging into pointers and classes in my Data Structures class. Why can I not compare the fitness variables of the tree objects using this sort?
I am getting the error: 'Environment::compare': non-standard syntax; use '&' to create a pointer to member.
void Environment::evolve(const int popsize,const int maxgen) {
std::cout << "> evolving function" << std::endl;
std::vector<Tree> popvec;
// Generate initial pop trees and store in array
for (int i = 0; i <= popsize - 1; i++) {
std::cout << "> generating initial population" << std::endl;
Tree membertree;
popvec.push_back(membertree);
}
// Loop for generations
for (int j = 1; j <= maxgen; j++) {
std::cout << "Generation " << j << ":" << std::endl;
// sort array by fitness
std::sort(popvec.begin(), popvec.end(), compare);
// cull herd
// reproduce
}
}
bool Environment::compare(Tree obj1, Tree obj2) {
return (obj1.fitness < obj2.fitness);
}
Thank you for your help.
Edit with header:
#include "Tree.h"
class Environment {
public:
Environment();
~Environment();
void evolve(const int popsize,const int maxgen);
bool compare(Tree obj1, Tree obj2);
private:
Tree* poparray;
};
The immediate cause of compile error
std::sort(popvec.begin(), popvec.end(), compare);
You're referencing compare, which is a class member function. To pass a pointer to class member (data or function) you need to use the &Class::Member syntax:
std::sort(popvec.begin(), popvec.end(), &Environment::compare);
But this won't work unless compare is a static function - as it should be (or you can do gymnastics with passing a stateful lambda/functor, but there's no reason a sane person would do this).
The real problem
You should change declaration of compare - make it a static member function:
static bool compare(Tree obj1, Tree obj2);
If you do this, changing the sort call syntax is no longer necessary (although still valid)
You'll have to declare Environment::compare as static, e.g.:
class Environment {
...
static bool compare (Tree obj1, Tree obj2);
...
};
You can describe a simple list as follows:
The simple list can hold zero or more items of some particular type.
You can create an empty list.
You can add items to the list.
You can determine whether the list is empty.
You can determine whether the list is full.
You can visit each item in the list and perform some action on it.
As you can see, this list really is simple; it doesn’t allow insertion or deletion, for example.
Design a List class to represent this abstract type.You should provide a list.h
header file with the class declaration and a list.cpp file with the class method
implementations.You should also create a short program that utilizes your design.
The main reason for keeping the list specification simple is to simplify this programming exercise.You can implement the list as an array or, if you’re familiar with
the data type, as a linked list. But the public interface should not depend on your
choice.That is, the public interface should not have array indices, pointers to nodes,
and so on. It should be expressed in the general concepts of creating a list, addingan item to the list, and so on.The usual way to handle visiting each item and performing
an action is to use a function that takes a function pointer as an argument:
void visit(void (*pf)(Item &));
Here pf points to a function (not a member function) that takes a reference to Item
argument, where Item is the type for items in the list.The visit() function applies
this function to each item in the list.You can use the Stack class as a general guide.
What I want to know is why should I use pointer-to-functions? what's the difference between using usual member function and using the function that uses pointer-to-function as argument? (in this case, using void visit(void (*pf)(Item &)))?
Function Pointers
Imagine you have a function which takes a number and squares it and returns it. And you have a list whose each member you want squared. How do you do that?
You either write another function which accepts an array, loops through it and converts every element to its square.
You have a function which accepts an array and a function which can transform one single element. You apply the function to each element of the array.
Both perform the same task. You might think the previous case is easier to implement. You wont have to deal with function pointers after all.
However what if you have say 20 functions which can double, triple, cube, square, etc. a single parameter passed to them. If you follow the first route, you have to write 20 different functions (with probably different names). However now the latter makes sense. You just declare the individual functions. And call the transformer function by passing the array and any of the 20 functions via pointer to achieve your task.
An example is std::transform in the C++ STL.
Working stuff :
#include <iostream>
#include <vector>
typedef double (*function)(double);
void transformer(std::vector<double>& to_transform, function f)
{
for(auto it = to_transform.begin(); it != to_transform.end(); ++it)
*it = f(*it);
}
void print(const std::vector<double>& v)
{
std::cout << "[ ";
for(double val : v)
std::cout << val << " ";
std::cout << "]" ;
}
double f1(double a) { return a*2; }
double f2(double a) { return a*3; }
double f3(double a) { return a/2; }
double f4(double a) { return a*a*a; }
int main() {
std::vector<double> array = { 2.3, 5.6, 4.5, 7.8, 2.3 };
std::vector<function> function_ptrs = { &f1, &f2, &f3, &f4 };
std::size_t val ;
std::cout << "The original : " ;
print(array);
std::cout << "\nChoose a function (1-4) : ";
std::cin >> val;
std::cout << "The array after applying function " << val << " is : ";
transformer(array, function_ptrs[(val - 1) % function_ptrs.size()]);
print(array);
return 0;
}
I am assuming you have a C++11 compliant compiler. The above code has 4 functions which take in a double and transform it somehow. The transformer function applies such a function to a vector of doubles. The function pointers are stored in a vector too - Yes an array of function pointers. The functions can be called as a normal element is accessed via indexing. On choosing an option, the appropriate function is called and executed by transformer element wise on the vector of doubles.
You can further improve it with templates (instead of fixed doubles) and using std::transform from STL.
C++11 Lambda Expressions
Also, with C++11, you should prefer lambdas and not function pointers. Lambdas are written as
[ ... capture list ... ] ( params ) -> return_type (optional) { body }
A solution with lambda would be something like this :
#include <iostream>
#include <algorithm>
#include <vector>
template <typename T>
void print(const std::vector<T>& v)
{
std::cout << "[ ";
for(T val : v)
std::cout << val << " ";
std::cout << "]" ;
}
int main() {
std::vector<double> array = { 2.3, 5.6, 4.5, 7.8, 2.3 };
std::cout << "The original : " ;
print(array);
std::cout << "\nThe array after transforming : " ;
std::transform(array.begin(), array.end(), array.begin(),
[](double x) { return x * x; });
print(array);
return 0;
}
Function Objects
You can declare your own class which just overloads the () operator (which makes the object callable) and does an identical job as a function pointer (can be passed to a function and called) i.e. in this case the class would look like :
class double_the_value
{
double operator()(double val) const { return val * 2.0 ; }
};
double_the_value f ;
std::cout << f(3.0) ; // outputs 6.0
An actual usage would be the std::unordered_map container where if you are using your own class types for keys, you will need to provide a key hasher - which can be a function object. This is demonstrated in detail by this answer.
When you're creating a true abstract list you have no idea what functions you will need to call on the objects, so your usual member functions won't be enough. You can't write all of them.
A common and easier alternative to this pattern is to return a copy of the full collection or expose iterators to the first and last element. But this can cause performance issues and can be risky - a copy can be expensive and is usually unnecessary, and if you use iterators they can become invalid if the collection changes beneath you. The visitor pattern hides all this by giving you better encapsulation and keeps the iteration loop within the class, where it usually belongs.
It's an additional layer of abstraction. If you have a collection of items, you usually want to do something to some (or all) of the items in that collection. The visitor pattern lets you inspect each item and then do something.
I have inherited my class from std::vector. Now I want to overload the []-operator.
When I try to assign a new value to my vector, e.g. v[0]=5, I should receive the message OK.
This is my code (I know, that makes no sense, I'm just playing around):
#include<vector>
#include<iostream>
class Vec : public std::vector<int> {
public:
int operator[](int);
};
int Vec::operator[](int i) {
(*this)[i] = i;
std::cout << "OK";
return 123;
}
int main() {
Vec v;
v[0]=5;
}
Unfortunately I get the following error:
In member function ‘int Vec::operator[](int)’:
error: lvalue required as left operand of assignmen
In function ‘int main()’:
error: lvalue required as left operand of assignment
This particular error is caused because you are not returning an lvalue, generally defined as something that can appear to the left of an assignment, such as v[0] = 5;. You have more problems as pointed out in the other answers but this is the specific issue you face with that error message (a).
The correct specification for overloading the index operator is:
int& operator[] (const int nIndex);
You have to return a reference to the item (so it can be modified) if you want to treat it as an lvalue. The following code shows a fix, although obviously all array indexes map to the same value in this simplified case:
#include <vector>
#include <iostream>
class Vec : public std::vector<int> {
public:
int& operator[] (int); // <-- note the '&'
private:
int xyzzy;
};
int& Vec::operator[] (int idx) { // <-- note the '&'
std::cout << "OK\n";
return xyzzy;
}
int main () {
Vec v;
v[0] = 5;
v[1] = 6;
std::cout << v[22] << '\n';
return 0;
}
The output of this is:
OK
OK
OK
6
In reality, you wouldn't map all indexes to the same value, the code above is simply to illustrate the correct function signature. I haven't bothered to give a more complete example since subclassing classes with non-virtual destructors regularly leads to problems in non-trivial code (b).
(a) It's not usually considered a good idea to subclass std::vector since the destructor isn't virtual, so you can get into trouble when trying to destroy an object polymorphically.
You're probably better off using a has-a relationship (where your class contains a vector) rather than an is-a relationship (where you inherit).
That unfortunately means you may have to create a lot of pass-through methods from your class to the underlying vector (although only the ones you need) but it will solve the problem with the destructor.
(b) See (a) :-)
You'd need to return a reference to your element - however note that even if you did, you'd run into inifinite recursion - your operator[] calls itself.
Either way - inheriting from std::vector isn't a good idea. Use composition instead.
The code below illustrates how to call the operator[] from the vector base class....
#include <iostream>
#include <vector>
struct Vec : std::vector<int>
{
int& operator[](int n)
{
std::cout << "operator[](" << n << ")\n";
return std::vector<int>::operator[](n);
}
};
int main()
{
Vec v;
v.push_back(10);
v.push_back(20);
v[0] += 5;
std::cout << v[0] << ' ' << v[1] << '\n';
}
Output when I run it:
operator[](0)
operator[](1)
operator[](0)
15 20
Don't take all this talk about "do not inherit from std::vector" too seriously: you have to go out of your way to delete a dynamically allocated Vec using a std::vector<int>*, or do an accidental by-value slicing copy - and even then it'd probably only bite you if you had added data members. You should make sure you understand those risks then make your own assessment, but for small utility programs etc. it's productive to inherit from such classes sometimes....