1
#ifndef DATACENTER_H_
#define DATACENTER_H_

#include <map>
#include <list>
#include <string>

#include "LiLo/SoundInfo.h"
#include "MutexCondition.h"
#include "UserInfo.h"

using namespace std;

class DataCenter : MutexCondition{

private:

    map<long long, list<SoundInfo *> > m_soundListMap;


    void add(long long deviceId, SoundInfo* soundInfo);

public:

    DataCenter();
    virtual ~DataCenter();

    static void addSoundInfo(long long deviceId, SoundInfo *soundInfo);

};

#endif /* DATACENTER_H_ */

DataCenter.cpp file

#include "DataCenter.h"

DataCenter::DataCenter() {
    // TODO Auto-generated constructor stub

}

DataCenter::~DataCenter() {
    // TODO Auto-generated destructor stub
}

void DataCenter::addSoundInfo(long long deviceId, SoundInfo *soundInfo){
    add(deviceId, soundInfo);
}

void DataCenter::add(long long deviceId, SoundInfo *soundInfo){
    list<SoundInfo*>& info_list = m_soundListMap[55];
}

I am trying to access the function call addSoundInfo from other classes so I have set this as static. Since the m_soundListMap is not a static so I think I need another function to access to the local data structure.

Inside of the static function, I call add function to add SoundInfo to the list. However, I am getting an error in the static function and it says "Can not call member function .... without object".

How do I fix this problem? Thanks in advance..

user800799
  • 2,883
  • 7
  • 31
  • 36
  • C++ doesn't have local functions (although there are workarounds for that, your code doesn't appear to have anything that could be described as a local function). – Ben Voigt Jun 23 '11 at 07:14
  • @Ben: But C++0x luckily does! :) – Xeo Jun 23 '11 at 07:15
  • @Xeo: No it doesn't. But the workarounds got slightly better. @Space: If you mean a function inside a class inside a function, that's what I meant by a workaround. And it isn't being done here. – Ben Voigt Jun 23 '11 at 07:17

5 Answers5

4

If you want to access addSoundInfo from other classes, you need to make it public, or make those other classes friends of DataCenter. static has nothing to with access control.

A static function is not bound to an instance of the class it belongs to, and thus can not access members of that class (it also can not call member-functions). If you really want to access members from a static function, you have to pass an instance of the class as argument the the static function explicitly.

If you struggle with such basic concepts, you should read a good book.

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
0

I suppose you mean you don't want to make void add() public and still you want to access it from some classes. It is nothing wrong with that and you can do it this way:


class A
{
private:
   void DoPrivateStuf() {}

   friend class B;  // now B can access A private stuf
};

class B
{
// can have any modifier: public, private, protected depending on your needs
public:

  void DoPrivateStufToA( A& a )
  {
     a.DoPrivateStuf();
  }
};


Sasha
  • 846
  • 5
  • 9
0

The code seems hopelessly jumbled, but technically you just need to remove the word static. Then you can call dc.addSoundInfo( id, pSoundInfo ) where dc is a DataCenter object.

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

static is a way to instruct the compiler "the following function is not manipulating instance variable, only things that are global to all the instances of this class". You use that when you need to keep your constructor private for some reason, or have a function that does instances management (registration, etc.)

When you intend to have only one instance of a given class (e.g. because it is a resource manager), you usually prefer to follow the singleton pattern: a static getInstance() method that return the only instance of that class and create it if needed, then you keep your other methods regular methods and your state instance members.

PypeBros
  • 2,607
  • 24
  • 37
0

As others have said, making addSoundInfo() public is enough for it to be available from other class. I'll just add some points about C++'s keyword static. Basically, it has many meanings depending on where is it used. When one uses it for functions, there are two meanings:

  • static class function: a function that is tied to a class, not any specific object. In this sense, it is similar to namespace concept - using the scope :: operator to access the function.
  • static function: The function has internal linkage, which means it is only visible in current translation unit (current source file). It is handy for utility functions.

In your case, the answer to your question will technically be something like this:

In the header file:

class DataCenter 
{
    static void addSoundInfo(DataCenter& dc, long long deviceId, SoundInfo *soundInfo);
}

In the source file:

void DataCenter::addSoundInfo(DataCenter& dc, long long deviceId, SoundInfo *soundInfo)
{
    dc.add(deviceId, soundInfo);
}

But it is probably not what you want.

Thanh DK
  • 4,257
  • 3
  • 23
  • 18