class inside namespace

Hey,

I briefly did some programming with VB.Net and liked how easy it was to ping a machine using System.Net.NetInformation namespace.

I've tried to implement something similar in C++ but having problems obviously.

1
2
3
4
5
6
7
8
9
10
11
12
namespace System {
  namespace Net {
    namespace NetworkInformation {
      class Ping {
        public:
          Ping();
          ~Ping();
          bool Send(std::wstring host);
      };
    }
  }
}


I declare the Send() method like

bool Network::Ping::Send(std::wstring host)

I want to call Send() like

 
System::Net::NetInformation::Send(L"www.google.com");


but Visual Studio throws an error:

error C2352: 'Network::Ping::Send' : illegal call of non-static member function

Sorry but i'm new to C++ and have tried various different things to try resolve on my own.
That to call a non-static membber function of a class you should create at first an object of the class and use it to call the function. Or you should declare the function as static.
Thanks vlad, that solved the method problem but another issue with linker.

I have a vector in the class also which neither works with or without being declared static.

I keep a list of responses from any host here

1
2
3
4
5
6
typedef struct _ICMP_REPLY {
  DWORD dwCode;
  std::wstring Message;
  std::wstring Dns;
  std::wstring Ip;
} ICMP_REPLY, *PICMP_REPLY;


Inside the Ping class

static std::vector<ICMP_REPLY> replies;

the linker is throwing error for this unfortunately.

error LNK2019: unresolved external symbol "public: static class std::vector<struct _ICMP_REPLY,class std::allocator<struct _ICMP_REPLY> > Network::Ping::replies"
Last edited on
I think that the problem is that you did not define the vector. You only declared it inside the class but not defined. It shall be defined somewhere in a module outside the class.
I understand what you mean now and as a result used a different way to achieve what i wanted which works great.

Initially, Ping class returned vector of ICMP_REPLY but i created separate class PingReply and just add each reply there.

cheers, vlad.
Topic archived. No new replies allowed.