class A{ public: template <typename T> void Log(T t); }; class B { A a; vector<C> vec; void func(){ vec.push_back(C(int d)); //a.Log(vec.back()); <-- проблема в этой строке } }; class C{ public: int D; C(int d){D=d;} }; 
  • one
    And where is the body of the A :: Log () method? - gbg
  • one
    The simple answer is that the fields and functions of classes are by default private; if they are not explicitly made public, they will not be visible from the outside. - insolor
  • @insolor is true, I forgot to indicate, in fact, A :: Log () public. - Dec
  • Well, the second a.Log < Tip_up > (vec.back ()); - nick_n_a
  • @nick_n_a The Same Error - Dec

1 answer 1

 #include<iostream> #include<vector> class A { public: template <typename T> void Log(T t) { // это для себя сделал что бы хоть что-то выводить std::cout << (static_cast<C>(t)).getD(); }}; class C { int D; public: int getD() { return D; } C(int d) { D = d; }}; class B { A a; std::vector<C> vec; public: void func(int d) { vec.push_back(C( d)); a.Log(vec.back()); }}; int main() { B obj; for (size_t i = 0; i <10; ++i) { obj.func(i); } system("pause"); } 
  • You have a.Log (vec.back ()); template method and in your function call this is not the case. a.Log <C> (vec.back ()) needs to be called with a type placeholder. I do not know your task, so I corrected the code a bit. void func () {vec.push_back (C (int d)); } because such a function doesn’t cause trust if you are honest. if you suddenly don’t like public access modifiers (although they are logical, you don’t have any reason, although you called private methods from other classes), then use friend-functions / classes. - Kirill21
  • You have a bad answer. The first is that C ++ can output template parameters by function signature, so a.Log <C> (vec.back ()) is not needed, a.Log (vec.back ()) is enough. Secondly, the author (and you) rearranged the declarations B and C, this will be the next compilation error. The third - the only serious problem of the author - the lack of body in the method of Log. Surely he put it in cpp, which the Fourth shouldn’t do, the fried fields in the class are a sign of bad architecture, do not advise them. - gbg
  • @gbg, with the Log method commented out body, the same error, therefore, did not indicate - Dec
  • @gbg, and why you should not thrust the implementation into the CPP, it’s logical not? - Dec
  • @gbg, I agree with all the points except 3 and 4 partially. In the third, he in fact has a body, albeit an empty one. And for the fourth, for example, one of the overloads of the input / output operator is friendly. Too bad architecture? At the expense of user classes - I agree the abuse of a friend is not good, but you should not refuse them. - Kirill21