I am not able to access private variables of class, I have used friend class - class

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Movie
{
private:
std::string name{};
std::string rating{};
int watched{};
friend class movies;
public:
Movie(std::string name_val, std::string rating_val, int watched_val = 0);
// copy constructor
Movie(const Movie &source); //// this constructor
};
Movie::Movie(std::string name_val, std::string rating_val, int watched_val)
:name{name_val}, rating{rating_val}, watched{watched_val}{}
Movie::Movie(const Movie &source){
name = source.name;
rating = source.rating;
watched = source.watched;
}
class Movies
{
private:
std::vector<Movie> movie_obj;
public:
void get_movie_byname(Movie &abc) { std::cout << abc.name; }; // here is error abc.name
};
here is the error message
main.cpp:53:53: error: 'std::string Movie::name' is private within this context
53 | void get_movie_byname(Movie &abc) { std::cout << abc.name; };
| ^~~~
main.cpp:11:21: note: declared private here
11 | std::string name {};
| ^~~~
cant understand whats wrong...
i think friend of a class is friend of for given obj ( this--> ), and not for all obj,
if i removed copy constructor of Movie, error resolved!
cant understand relation between copy constructor and friend.
// issue solved turns out its just typo in Movies, declaration in friend declaration.

Related

boost.python MSVC12 linker errors when exposing classes with docstring_options

I regularly expose c++ classes to python using boost.python & MSVC 12 (dynamic linking). Recently I have been trying to include documentation using the "docstring_options" class. The documentation examples work fine:
http://www.boost.org/doc/libs/1_54_0/libs/python/doc/v2/docstring_options.html
However, when I include a class and expose it I get linker errors:
error LNK2019: unresolved external symbol "void __cdecl boost::throw_exception(class std::exception const &)" (?throw_exception#boost##YAXABVexception#std###Z) referenced in function "public: __thiscall boost::detail::shared_count::shared_count(void *,struct boost::python::converter::shared_ptr_deleter)"
I'm sure there is probably something simple I'm missing but I can't figure it out.
Many thanks in advance!
sample code spliced from the boost examples that gives this error for me.
#include <string>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/args.hpp>
#include <boost/python/docstring_options.hpp>
#include <boost/python.hpp>
struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
int foo1(int i) { return i; }
int foo2(long l) { return static_cast<int>(l); }
int bar1(int i) { return i; }
int bar2(long l) { return static_cast<int>(l); }
namespace {
void wrap_foos()
{
using namespace boost::python;
def("foo1", foo1, arg("i"), "foo1 doc");
def("foo2", foo2, arg("l"), "foo2 doc");
}
void wrap_bars()
{
using namespace boost::python;
bool show_user_defined = true;
bool show_signatures = false;
docstring_options doc_options(show_user_defined, show_signatures);
def("bar1", bar1, arg("i"), "bar1 doc");
def("bar2", bar2, arg("l"), "bar2 doc");
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}
}
BOOST_PYTHON_MODULE(boost_py_doc_demo)
{
boost::python::docstring_options doc_options(false);
wrap_foos();
wrap_bars();
}
I compiled the latest version of boost (1.63) and now the problem has gone. I guess my old libraries were incomplete in some way.

How to use references and pointers in c++ classes?

i have the following problem: I am using an existing class which creates an object called server_t.
Another function expects *server_t as an argument.
I wanted to shrink the code and added a class which has following members:
#ifndef _PMCLASS
#define _PMCLASS
#include "pmlib.h"
class pmServer{
private:
server_t server ;
counter_t counter;
line_t lines;
server_t * server2;
int set, frequency, aggregate ;
public:
pmServer();
pmServer(int set, int frequency, int aggregate);
~pmServer();
void setSet(int s);
void setFrequency(int f);
void setAggregate(int a);
int getSet(void);
int getFrequency(void);
int getAggregate(void);
server_t* getServerT(void);
counter_t* getCounterT(void);
line_t* getLineT(void);
server_t* getZeiger(void);
};
#endif
then i created the constructors:
#include "pmClass.h"
#include <iostream>
using namespace std;
void pmServer::setSet(int s){
this->set = s;
}
void pmServer::setFrequency(int f){
this->frequency = f;
}
void pmServer::setAggregate(int a){
this->aggregate = a;
}
int pmServer::getSet(void){
return set;
}
int pmServer::getFrequency(void){
return frequency;
}
int pmServer::getAggregate(void){
return aggregate;
}
server_t* pmServer::getPointer(){
return &server;
}
pmServer::pmServer(){
set = -1;
frequency = 0;
aggregate = 1;
}
then i tried to create an object ->worked, but then i wanted to use the pm_set_server(...)
it wants following arguments: int pm_set_server( char *ip, int port, server_t *pm_server)
void run() {
build_initial_mesh();
// Construct / read in the initial mesh.
pmServer server1;
pm_set_server("xxx.xxx.xxx.xxx", 6526,server1.getPointer); //its a correct ip address , no panic :)
i got that:
error: argument of type 'server_t*' (pmServer::)() does not match 'server_t*'
but this worked without any problems:
void run() {
// Construct / read in the initial mesh.
//pmServer server1;
server_t test;
pm_set_server("xxx.xxx.xxx.xxx", 6526,&test);
build_initial_mesh();
The thing is, i didn't want to create everytime new ojects and wanted to do that in the constructor...Does somebody have any idea?
thanks.
greetings Thomas
In C++, function calls need brackets*:
pm_set_server("xxx.xxx.xxx.xxx", 6526,server1.getPointer());
*exceptions apply, for operators.

Unmanaged method member being used as a delegate in managed class

I have a project where I am expanding existing native C++ compatible code functionality for a customer-facing library by integrating a driver from a managed DLL, which seems like the opposite direction for most marshaling questions. Since there can be multiple devices using this one driver, each device needs to be its own object, with its own callback methods. This integration will take the form of a native wrapper DLL that exposes the native class interface to the customer-facing library, which will pass an instance of a member method callback function pointer to the DLL. I need to pass a native method member instance (non-static) as a delegate to the managed wrapper class. The managed wrapper class uses GetDelegateForFunctionPointer, which works to marshal static native pointers to a delegate, but I can't find any information if it works with instances. GetDelegateForFunctionPointer also takes a System::IntPtr argument for the function pointer, but I'm also not sure if System::IntPtr works for instances, and when compiling in VC++2008 I'm being presented an error C3867 for attempting this.
Managed wrapper header:
//somemanagedclass.hpp
#pragma once
using namespace System;
using namespace ExternalManagedLibrary;
namespace SomeNameSpace
{
public ref class SomeManagedClass
{
public:
SomeManagedClass();
~SomeManagedClass();
delegate void CallbackHandler(const wchar_t*, int);
CallbackHandler^ CallbackEvent;
void RegisterCallback(IntPtr callbackEvent);
private:
ExternalManagedClass^ externalManagedClass;
void OnCallback(Object^ sender, ValueEventArgs<String^>^ e);
};
}
Managed wrapper source:
//somemanagedclass.cpp
#include "somemanagedclass.hpp"
#include <vcclr.h>
using namespace System;
using namespace Runtime::InteropServices;
namespace SomeNameSpace
{
SomeManagedClass::SomeManagedClass()
{
externalManagedClass = gcnew ExternalManagedClass();
externalManagedClass->CallbackEvent += gcnew EventHandler<ValueEventArgs<String^>^>(this, &SomeManagedClass::OnCallback);
}
SomeManagedClass::~SomeManagedClass()
{
externalManagedClass->CallbackEvent -= gcnew EventHandler<ValueEventArgs<String^>^>(this, &SomeManagedClass::OnCallback);
}
void SomeManagedClass::OnCallback(Object^ sender, ValueEventArgs<String^>^ e)
{
String^ some_string = String::Copy(e->Value);
cli::pin_ptr<const wchar_t> pinned_string = &PtrToStringChars(some_string)[0];
const wchar_t* p = pinned_string;
CallbackEvent(pinned_string, some_string->Length);
}
void SomeManagedClass::RegisterCallback(IntPtr callbackEvent)
{
CallbackEvent = (CallbackHandler^)(Marshal::GetDelegateForFunctionPointer(callbackEvent, CallbackHandler::typeid));
}
}
Native wrapper interface:
//somenativeinterface.hpp
#ifdef DLL_EXPORT
#define IMPORT_EXPORT __declspec(dllexport)
#else
#define IMPORT_EXPORT __declspec(dllimport)
#endif //DLL_EXPORT
typedef void (*NativeCallback)(const unsigned char*, unsigned long);
class IMPORT_EXPORT SomeNativeInterface
{
public:
//class factory
static SomeNativeInterface* Create(void);
static void Destroy(SomeNativeInterface* clInterface);
virtual void CallbackInit(NativeCallback fnNativeCallbackInit);
};
Native wrapper header:
//somenativeclass.hpp
#pragma once
#include "somenativeinterface.hpp"
#include "somemanagedclass.hpp"
#include <vcclr.h>
using namespace SomeNameSpace;
class IMPORT_EXPORT SomeNativeClass : public SomeNativeInterface
{
public:
SomeNativeClass();
~SomeNativeClass();
void CallbackInit(NativeCallback fnNativeCallbackInit); //can this take an instance?
private:
NativeCallback fnNativeCallback;
void OnNativeCallback(const wchar_t* cString, int iSize);
gcroot<SomeManagedClass^> wrapper; //warning C4251
};
Native wrapper source:
//somenativeclass.cpp
#include "somenativeclass.hpp"
#include <vcclr.h>
#include <string.h>
using namespace System;
using namespace Runtime::InteropServices;
SomeNativeInterface* SomeNativeInterface::Create()
{
return ((SomeNativeInterface*) new SomeNativeClass());
}
void SomeNativeInterface::Destroy(SomeNativeInterface* instance)
{
delete instance;
}
SomeNativeClass::SomeNativeClass()
{
wrapper = gcnew SomeManagedClass();
}
SomeNativeClass::OnNativeCallback(const wchar_t* cString, int iSize)
{
std::auto_ptr<char> pcConvertedString(new char[iSize+1]);
size_t iCharsConverted;
if (wcstombs_s(&iCharsConverted, (char*)*pcConvertedString, iSize+1, cString, iSize) == 0)
{
if (iCharsConverted > 0xFFFFFFFF)
iCharsConverted = 0xFFFFFFFF; //truncate
fnNativeCallback((const unsigned char*)*pcConvertedString, (unsigned long)(iCharsConverted));
}
}
SomeNativeClass::CallbackInit(NativeCallback fnNativeCallbackInit)
{
fnNativeCallback = fnNativeCallbackInit;
wrapper->RegisterCallback(System::IntPtr(this->OnNativeCallback)); //error C3867
}

Access to the same class instance from qml

I'm using Qt 5.1.0 and QtQuick 2.0.
I've just learned how to exchange data and method between qml and C++. In detail, I want to expose a (nested) structure, so I created classes which contains the required properties and methods.
It works, but I cannot share the very same instance between C++ and qml. Changing something from C++ side leads to no changes in qml side.
A short example:
main.cpp
#include <QtGui/QGuiApplication>
#include <QtQml>
#include "qtquick2applicationviewer.h"
#include "myclass.h"
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
qmlRegisterType<MyClass>("MyLibrary", 1, 0, "MyClass");
MyClass *myclass = new MyClass();
Category *cat = new Category();
cat->setName("foo");
myclass->append(cat);
qDebug() << myclass->categoriesCount(); // returns 1 OK!
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/main.qml"));
viewer.show();
return app.exec();
}
myclass.h
#include <QObject>
class Category : public QObject {
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
public:
Category() {}
QString name() const { return m_name; }
void setName(QString name) { m_name = name; }
private:
QString m_name;
};
class MyClass : public QObject {
Q_OBJECT
Q_PROPERTY(QList<QObject*> categories READ categories NOTIFY categoriesChanged)
Q_PROPERTY(int categoriesCount READ categoriesCount)
public:
QList<QObject*> categories() const { return m_categories; }
void clear() { m_categories.clear(); }
void append(Category *category) { m_categories.append(category); }
int categoriesCount() const { return m_categories.count(); }
private:
QList<QObject*> m_categories;
signals:
void categoriesChanged();
};
main.qml
import QtQuick 2.0
import MyLibrary 1.0
Item {
id: root
width: 360
height: 360
Text {
id: foo
text: myclass.categoriesCount // writes 0 -> should be 1
}
MyClass {
id: myclass
}
}
I'm afraid the qml engine accesses to a different instance of the class myclass created in the main file.

Is it possible to point a Typedef function pointer to a class member?

I am working with an executable that includes a DLL. For my testcase, I combined the code into a single executable. I am working with Visual Studio 2008 and Boost 1.43. I've tried researching this, but haven't found any clear answer. Thanks for the help.
In my main.h:
#include <string>
//These are normally defined in a seperate DLL
typedef std::string Typedef_func(const std::string & title);
void Register_My_Typedef(Typedef_func*);
//-------------------------------------------
class myClass
{
public:
std::string func_one(const std::string & title);
Typedef_func _test;
void run();
};
In my main.cpp:
#include "main.h"
#include <boost/bind.hpp>
std::string workingFunc(const std::string & title)
{
return "";
}
int main(int argc, char* argv[])
{
myclass* example;
example->run();
Register_My_Typedef(&workingFunc);//This works.
return 0;
}
void myClass::run()
{
//I want to point a Typedef_func* in a DLL to call myclass::func_one
Typedef_func* tf = boost::bind(&myClass::func_one, this, "test"); //This does not.
Register_My_Typedef(tf);
}
std::string myClass::funcOne(const std::string & title)
{
return "";
}
void Register_My_Typedef(Typedef_func* passedIn)
{
//Points the pointer in the DLL to passedIn
}
The DLL logic works fine when Register_My_Typedef is called on a function not in a class, but is it possible to call it from within a class? When I try to compile this code it returns:
When I try and compile in Windows XP with VS2008 I get:
Error C2440: 'initializing' : cannot convert from
'boost::_bi::bind_t' to 'Typedef_func (__cdecl *)' with
[
R=std::string,
F=boost::_mfi::mf1,
L=boost::_bi::list2,boost::_bi::value>
]
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called.
The Answer is Typedef itself is class member for a static member function & behave differently to non-static however works best reading rather class function of user defined class main.