I have two .cpp files and one header ".h". I decided to output the GenCan1 result to main, but I get this error (Error C3867 "TElection :: GetCan1": nonstandard syntax; use "&" to create a pointer to a member)

main.cpp #include <iostream> #include <EClass.h> #include <stdafx.h> using namespace std; int main() { std::cout << TElection::GetCan1<<std::endl; system("pause"); } EClass.h class TElection { protected: float can1, can2, can3; public: TElection(); TElection(TElection &el); TElection(float a, float b, float c); ~TElection(); float GetCan1(); void SetCan1(float i); }; Election.cpp #include <iostream> #include <EClass.h> #include <stdafx.h> TElection::TElection() { can1 = 0; } TElection::TElection(TElection& el) { can1 = el.can1; } TElection::TElection(float a, float b, float c) { can1 = a; } TElection::~TElection() { can1 = -1; } float TElection::GetCan1() { return(can1); } void TElection::SetCan1(float i) { if ((i >= 10) && (i < 0)) can1 = 10; else can1 = i; std::cout << "can1=" << can1; } 
  • Add () to denote a function call. And also read about how to give the code in the questions: the minimum reproducible example . - 伪位蔚蠂慰位蠀蟿
  • if you put (), an error is issued (a non-static link not a member should be specified relative to the specified object) - Silver
  • Well, create an object of a given class, which means that the function is called for :) - 伪位蔚蠂慰位蠀蟿
  • Well, I want to call the result of the class float TElection :: GetCan1 (). Indeed, in SetCan1, the value can1 is set and displayed using the GetCan1 class. - Silver
  • The GetCan1() function is not static static , which means that in order to be able to call it, you need to provide an instance of the TElection class, create an object, i.e. For example, like this: TElection obj; continue to call the obj function: obj.GetCan1() . All this is said in any textbook on language in its basic part. - 伪位蔚蠂慰位蠀蟿 pm

1 answer 1

If you really want to display the result , add parentheses and an object of the class for which the non - static member function is called:

 std::cout << obj.GetCan1()<<std::endl; 

and so

 std::cout << TElection::GetCan1<<std::endl; 

you output the address of the member function TElection::GetCan1 , and for this you really need the operator to take the address & .

  • Thanks a lot - Silver