• Welcome to Valhalla Legends Archive.
 

C 2352 trouble

Started by jrains86, December 17, 2009, 05:54 PM

Previous topic - Next topic

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;
}

rabbit

First, instantiate buddies.  Second, where's the body of the class?  Third, don't use ::, use ->.  Fourth, do you know what C2352 is?
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

MyndFyre

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: buddies.makefriends(buddies.names[0], buddies.names[1]) 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 people buddies; he's creating a stack-based instance of the object; he's just not initializing it so it'll have garbage.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

rabbit

Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.