Author | Message | Time |
---|---|---|
jrains86 | So I'm trying to get some C++ under my belt before classes resume in January, and have created a simple class to get comfy using them. However, when I try to call the function makefriends, I get the C2352 error message, and do not understand the why. Any insight into my predicament? My code is as follows: #include <stdafx.h> #include <iostream> #include <string> #define SZ 10 using namespace std; class people { public: string names[SZ]; void makefriends(string, string); }; int main() { people buddies; int num; cout<<"Enter the name of a friend."<<endl; getline(cin, buddies.names[0]); cout<<"Enter another friend's name."<<endl; getline(cin, buddies.names[1]); people::makefriends(buddies.names[0], buddies.names[1]); cin>>num; return 0; } | December 17, 2009, 11:54 PM |
rabbit | First, instantiate buddies. Second, where's the body of the class? Third, don't use ::, use ->. Fourth, do you know what C2352 is? | December 18, 2009, 1:29 PM |
Myndfyr | From http://msdn.microsoft.com/en-us/library/2x426hte(VS.80).aspx - Compiler Error C2352 Error Message 'class::function' : illegal call of non-static member function A static member function called a nonstatic member function. Or, a nonstatic member function was called from outside the class as a static function. It's because you're calling people::makefriends as a static function, but it's really a class instance function. As rabbit pointed out, you'd need to call it via your "buddies" object (although, instead of using the implicit dereference operator, you should use the dot operator: [tt]buddies.makefriends(buddies.names[0], buddies.names[1])[/tt] since "buddies" is not a pointer). @rabbit: Recall that C++ isn't like Java or C# in that you don't have to "new" an object in order to have an instance of it. By declaring [tt]people buddies;[/tt] he's creating a stack-based instance of the object; he's just not initializing it so it'll have garbage. | December 18, 2009, 2:36 PM |
rabbit | Yeah well.... :P | December 18, 2009, 4:51 PM |