how to print a list<class*> ls? - c++

What am I doing wrong at the last two lines in my code below? I receive an error:
request for member ‘name’ in ‘t.std::_List_iterator<_Tp>::operator* [with _Tp = a*]()’, which is of non-class type ‘a*’
#include <dlfcn.h>
#include <iostream>
#include "/home/developer/Desktop/MsgSphSDK1/test1_sdk/libsdk_MS.hpp"
#include <list>
#include <typeinfo>
#include <string>
using namespace std;
class a
{
public:
string name;
int age;
};
int main()
{
a* l = new a();
l->name = "me";
l->age = 1;
list<a*> ls;
list<a*>::iterator t;
for (t = ls.begin(); t != ls.end(); ++t)
cout << (*t).name << endl;
}

You should write
cout<<(*t)->name<<endl
t is an iterator, (*t) gives you a* (the pointer to the object of class a). So to get access to the member of the object you should use ->, not ..

Related

How to correctly do the following: passing unique_ptr to lambda, then passing this lambda somewhere else

I have written this code, meaning that the lambda will own the unique pointer, but I also have to pass this lambda to other function to be called there.
#include <memory>
#include <string>
#include <iostream>
#include <functional>
using namespace std;
void call(function<void()>&& f) {
f();
}
int main()
{
auto ptr = make_unique<string>("hello\n");
auto fn = [ptr = move(ptr)] () { cout << *ptr; };
call(move(fn));
return 0;
}
I got errors like
error: use of deleted function ‘main()::<lambda()>::<lambda>(const main()::<lambda()>&)’
error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = std::__cxx11::basic_string<char>; _Dp = std::default_delete<std::__cxx11::basic_string<char> >]’
I'm looking for the way how to do what I need correctly.
The closure, lambda is not a std::function<void()>, this is the issue. The compiler needs to create a temporary std::function<void()>. The working code
#include <memory>
#include <string>
#include <iostream>
#include <functional>
using namespace std;
void call(auto&& f) {
f();
}
int main()
{
auto ptr = make_unique<string>("hello\n");
auto fn = [ptr = move(ptr)] () { cout << *ptr; };
call(move(fn));
return 0;
}

How access class functions with <future> library?

I'm trying to make a program with #include <future> library.
When I try to access a header function, I get an error.
no instance of overloaded function "async" matches the argument list -- argument types are: (std::launch, int ()) [line 16, 14]
a pointer to a bound function may only be used to call the function
[line 16, 37]
main.cpp:
#include "TEST.h"
#include <future>
#include <iostream>
using namespace std;
using namespace Class;
FNH f;
int main(){
auto fn = async(launch::async, f.selam);
}
TEST.h:
#pragma once
#include <iostream>
using namespace std;
namespace Class{
class FNH{
public:
int selam(){
cout << "selam";
return 1;
}
};
}
I'm a beginner at coding so I really don't know how to fix it or if it's possible.
You can pass a member function pointer and the instance of the class it will be called on:
#include <future>
#include <iostream>
namespace Class {
class FNH {
public:
int selam(){
std::cout << "selam";
return 1;
}
int selam_2(int a, int b){
std::cout << "selam "<< a << " " << b;
return 1;
}
};
}
int main(){
Class::FNH f;
// Member Function Pointer
auto fn = std::async(std::launch::async, &Class::FNH::selam, f);
// Member Function Pointer with arguments
auto fn2 = std::async(std::launch::async, &Class::FNH::selam_2, f, 1, 2);
}
Put the method call in a lambda
#include <future>
#include <iostream>
namespace Class{
class FNH {
public:
int selam(){
std::cout << "selam";
return 1;
}
};
}
int main(){
Class::FNH f;
auto fn = std::async(std::launch::async, [&f]{ return f.selam(); });
}

Using rolling accumulators in unordered maps in a Class

I'm trying to use an unordered map to hold rolling accumulators in a class.
First let me show what works. Here is an accumulator within a class that works as expected without the map. Note that the accumulator needs to be initialized in the initialization list.
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
namespace nmbstacc = boost::accumulators;
typedef nmbstacc::accumulator_set<double, nmbstacc::stats<nmbstacc::tag::rolling_mean >> MACC;
class RollMean {
public:
MACC m_acc;
RollMean(void) : m_acc(nmbstacc::tag::rolling_window::window_size = 3) {}
};
int main()
{
RollMean obj;
obj.m_acc(0.5);
obj.m_acc(1.5);
obj.m_acc(2.5);
obj.m_acc(3.5);
std::cout << "roll_mean: " << nmbstacc::rolling_mean(obj.m_acc) << std::endl;
std::getchar();
return 0;
}
However, what I need is an unordered map to hold these accumulators in a class but can't seems to figure out how to get the following program to compile. I'm not sure how to declare the mainmap container without first initializing the rolling accumulator.
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
#include <unordered_map>
namespace nmbstacc = boost::accumulators;
typedef nmbstacc::accumulator_set<double, nmbstacc::stats<nmbstacc::tag::rolling_mean >> MACC;
class RollMean {
public:
MACC m_acc;
std::unordered_map<std::string, MACC> mainmap;
RollMean(std::string name) : m_acc(nmbstacc::tag::rolling_window::window_size = 3) {
mainmap.emplace(name, m_acc);
}
};
int main()
{
RollMean obj("a");
obj.mainmap["a"](1.0);
std::cout << "roll_mean: " << nmbstacc::rolling_mean(obj.mainmap["a"]) << std::endl;
std::getchar();
return 0;
}
I get the following error:
Error C2679 binary '[': no operator found which takes a right-hand operand of type 'boost::parameter::keyword' (or there is no acceptable conversion)
Thanks.
Like #jv_ hinted, map[key] is a mutating operation, which inserts a default constructed element if none exists.
However, there's no default constructor for your element type. Therefore, you can't use that operator.
If you use obj.mainmap.at("a") instead of obj.mainmap["a"], you'll get an exception on missing keys instead.
Live On Coliru
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
#include <unordered_map>
namespace nmbstacc = boost::accumulators;
typedef nmbstacc::accumulator_set<double, nmbstacc::stats<nmbstacc::tag::rolling_mean> > MACC;
class RollMean {
public:
MACC m_acc;
std::unordered_map<std::string, MACC> mainmap;
RollMean(std::string name) : m_acc(nmbstacc::tag::rolling_window::window_size = 3) { mainmap.emplace(name, m_acc); }
};
int main() {
RollMean obj("a");
obj.mainmap.at("a")(1.0);
std::cout << "roll_mean: " << nmbstacc::rolling_mean(obj.mainmap.at("a")) << std::endl;
}
Prints:
roll_mean: 1

C++ Error: conversion to non-scalar type requested?

I'm fairly new to C++ and i'm trying to build a linked list with a container class called FlexString. In main() I want to instantiate the FlexString class by simply saying: "FlexString flex_str = new FlexString();" calling the constructor etc. But it won't compile, the error is at the bottom. Here is my code:
//FlexString.h file
#ifndef FLEXSTRING_CAMERON_H
#define FLEXSTRING_CAMERON_H
#include "LinkedList.h"
#include <string>
using namespace std;
using oreilly_A1::LinkedList;
namespace oreilly_A1 {
class FlexString {
public:
FlexString();
void store(std::string& s);
size_t length();
bool empty();
std::string value();
size_t count();
private:
LinkedList data_list;
};
}
#endif
Here is the .cpp file for the FlexString class:
#include "FlexString.h"
#include "LinkedList.h"
#include <string>
using namespace std;
namespace oreilly_A1 {
FlexString::FlexString() {
}
void FlexString::store(string& s) {
data_list.list_head_insert(s);
}
std::string value() {
data_list.list_getstring();
}
}
Here's the main program file.
#include <iostream>
#include <cstdlib>
#include "FlexString.h"
using namespace std;
using oreilly_A1::FlexString;
int main() {
FlexString flex_str = new FlexString();
cout << "Please enter a word: " << endl;
string new_string;
cin >> new_string;
flex_str.store(new_string);
cout << "The word you stored was: "+ flex_str.value() << endl;
}
error: conversion from 'oreilly_A1::FlexString*' to non-scalar type 'oreilly_A1::FlexString' requested. "FlexString flex_str = new FlexString();"
FlexString flex_str = new FlexString();
is wrong since the RHS of the assignment is a pointer to a FlexString while the LHS is an object.
You can use:
// Use the default constructor to construct an object using memory
// from the stack.
FlexString flex_str;
or
// Use the default constructor to construct an object using memory
// from the free store.
FlexString* flex_str = new FlexString();

How to access member variable in boost lambda expression?

#include <functional>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <boost/lambda/lambda.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost::lambda;
using namespace boost::multi_index;
using boost::multi_index_container;
using boost::lexical_cast;
struct student {
int no;
string name;
student(int _no, string _name) : no(_no), name(_name) { }
};
struct no {};
struct name {};
struct change_name {
change_name(const string& new_namex) : new_name(new_namex) {}
void operator()(student& stu) {
stu.name = new_name;
}
private:
string new_name;
};
int main()
{
typedef multi_index_container<
student,
indexed_by<
ordered_non_unique<tag<no>, member<student, int, &student::no>>,
ordered_non_unique<tag<name>, member<student, string, &student::name>>
>> student_set;
typedef student_set::index<no>::type student_set_no;
student_set students;
for (int i = 0; i < 10; ++i)
students.insert( student(i, "love student " + lexical_cast<string>(i)) );
student_set_no& no_index = students.get<no>();
student_set_no::iterator iter = no_index.find(3);
//suppose i want to change the name to "you",
no_index.modify(iter, change_name("you"));
//how could i do this with lambda expression ?
no_index.modify(iter, _1.name = "you");
cout << iter->name << endl;
return 0;
}
How could I change the name without defined the function operator class?
Thanks.