Using class in CPP compiler

Discussion to talk about software related topics only.
Post Reply
montambaultp
Posts: 5
Joined: Sun Jun 09, 2013 6:21 pm

Using class in CPP compiler

Post by montambaultp »

Hi

I'm new user with the Netburner Platform. I try to create a class and use the new operator to create my object. The code for the class is:

Code: Select all

#ifndef CRTC_H_
#define CRTC_H_

namespace SAN {

class cRTC {
public:
	cRTC();
	virtual ~cRTC();

};

The main program use the class:

Code: Select all

#include "predef.h"
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <taskmon.h>
#include <NetworkDebug.h>
#include <crtc.h>
//using namespace SAN;


extern "C" {
void UserMain(void * pd);
}

const char * AppName="SAN_0_0";

void UserMain(void * pd) {
    InitializeStack();
    OSChangePrio(MAIN_PRIO);
    EnableAutoUpdate();
    StartHTTP();
    EnableTaskMonitor();

    #ifdef _DEBUG
    InitializeNetworkGDB_and_Wait();
    #endif

    cRTC objRTC = new cRTC();

    iprintf("Application started\n");
    while (1) {
        OSTimeDly(20);
    }
}


} /* namespace SAN */
#endif /* CRTC_H_ */
The compiler generate an error :

..\main.cpp:29: error: 'cRTC' was not declared in this scope
..\main.cpp:29: error: expected `;' before 'objRTC'

I don't see my error, can you help me?

Thank you

Pat
dpursell
Posts: 20
Joined: Thu Aug 05, 2010 3:15 pm

Re: Using class in CPP compiler

Post by dpursell »

Are you intentionally leaving the SAN namespace open in the header and completing it in the main.cpp? I'm not sure this is what you want to be doing, unless it's a C++ technique I've never seen.

Generally you want to close namespaces out in the header, so you will have this as your header:

Code: Select all

#ifndef CRTC_H_
#define CRTC_H_

namespace SAN {

class cRTC {
public:
   cRTC();
   virtual ~cRTC();
};

} // end namespace SAN

#endif // close out ifndef guard
And then to create an object in main.cpp:

Code: Select all

SAN::cRTC objRTC; // Note: no "new" unless you make your object a pointer e.g. SAN::cRTC* objRTCptr = new SAN::cRTC;
Edit: You'll also want to take out the namespace-ending '}' at the end of your main.cpp since it's now closed out in the header
montambaultp
Posts: 5
Joined: Sun Jun 09, 2013 6:21 pm

Re: Using class in CPP compiler

Post by montambaultp »

Thank you for your assistance. The namespace in header file was correct but with the copy-paste method, the end bracket was not present in the post. But the call to create an object was not write properly.

Thank you
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: Using class in CPP compiler

Post by tod »

I see several problems. First you only show the declaration of your class, I'm assuming that what you showed is crtc.h. Do you have a crtc.cpp? If not then you at least need to modify your .h to make your declarations definitions. Here's how you would do that for a constructor

Code: Select all

cRTC(){}; //note the open and close braces
Also in the code you show I don't see the closing brace for the namespace.

Second your class lives in a namespace so you either need to qualify with that namespace or include the commented out 'using' that you have in your main file. The explicit form would be
SAN::cRTC objRTC = new SAN::cRTC();

Whiles it's not a problem, you can simplify your test and not bother (at least at first) with allocating the object on the heap. You can just make a solid object of cRTC right on the stack via (i.e. no new keyword).

cSAN::cRTC objRTC;


It looks like you're trying to learn C++ on the NetBurner. I think this would be unnecessarily difficult. If you just want to use the provided NB Tools and first learn C++ and then learn the NetBurner you can download MinGW and develop apps for your desktop. My Eclipse Guided Tour course on Pluralsight will show you the details on installing and configuring MinGW and getting a basic hello world up and running. You can sign up for the free trial and watch. I also have a series of 38 C++ videos I did for a high school robotics class I was teaching. They are much rougher than the Pluralsight course (all done it a single take no editing, warts and all), but you might find int helpful. (I turned on monetization after the class was over so you will have to deal with watching ads, but they can be skipped after a few seconds).
Post Reply