Greetings,
I'm kind of new to C++ and am having difficulty understanding subject I haven't explored thoroughly. Some kind souls helped me for a side-project but it seems I've reached their patience threshold and am hoping you can provide me a bit more patience on the matter ; perhaps I'd be in luck to find people excited to show the subject.
The purpose of this side-project if to have 2 ways of displaying a class. The algorithm behind those 2 display functions is quite similar but have a big impact on the object ; in fact it can very easily have a helper function which have a single Boolean parameter.
I had much difficulty constructing the following from the hints that were provided to me and at the end, an especially generous IRC user completed the code correctly ; yielding this which display. I made some modifications simplifying it, so I wouldn't have to duplicate further modifications attempting to integrate template.(blah)[blah]
which displayCode:#include <iostream> #include <string> struct foowrapper2; class foo { std::string name; public: void setName(std::string iniName) { name = iniName; } // std::string getName() const { // return name; // } friend std::ostream & operator<<(std::ostream &, foowrapper2 const &); friend foowrapper2 print2(foo const &); }; struct foowrapper2 { foo const & objFoowrapper2; }; foowrapper2 print2(foo const & objPrint2) { return {objPrint2}; // without curly brackets: error: conversion from ‘const foo’ to non-scalar type ‘foowrapper2’ requested } std::ostream & operator<<(std::ostream & lhs, foowrapper2 const & rhs) { lhs << '[' << rhs.objFoowrapper2.name << ']'; // rhs.objFoowrapper2.getName() } int main() { foo f; f.setName("blah"); std::cout << print2(f); }At the time the wrapper code was given to me, I forgot to mention the object was in a template so today I asked some more help. I was told that as my object used a template, I need to make the wrapper templated. I got some pointers, making the appropriate modifications up to the last one which I couldn't get and exhausted their patience. Notice the commented error messages in the following:[blah]
I was told that as the wrapper containCode:#include <iostream> #include <string> template <typename T> class foo { public: T id; void setId(T iniName) { id = iniName; } }; template <typename T> struct foowrapper { foo<T> const & objFoowrapper; }; template <typename T> foowrapper print(foo<T> const & objprint) { // error: invalid use of template-name ‘foowrapper’ without an argument list return {objprint}; // without curly brackets: error: conversion from ‘const foo’ to non-scalar type ‘foowrapper’ requested } std::ostream & operator<<(std::ostream & lhs, foowrapper const & rhs) { // error: expected constructor, destructor, or type conversion before ‘&’ token lhs << '[' << rhs.objFoowrapper.id << ']'; } int main() { foo<std::string> f; f.setId("blah"); std::cout << print(f) << std::endl; // error: ‘print’ was not declared in this scope }, it need to be instantiated. Well I don't understand, in the main function (at the bottom), I declaredCode:foo<int> const & objFoowrapperwhich I thought would be passed to the wrapper as there's a reference to it. I don't know what the reference mean between the wrapper and class, I only know it create some kind of relationship between them ; allowing to have a many-to-one relationship from wrapper to class.Code:foo<std::string> f;
Thank you kindly for your help



LinkBack URL
About LinkBacks




Tthis surely is miles ahead of me. I don't see the declaration of PrettyTypeIndex() but perhaps I'm distracted. This is like string modifiers, I wonder what's the name? Plain modifier? I like this way better!
I don't speak French unfortunately, but it's good to see some Canadians using this forum.
CornedBee