Jump to content
katang

How to define constructor in cpp file?

Recommended Posts

In the book "SystemC from the Ground Up" book, page 57,

an implementation style is suggested which seems to be reasonable. However, when I attempt to compile it, I am presented with the message

 error: expected constructor, destructor, or type conversion before '(' token
 NAME::NAME(sc_module_name nm)
           ^

Did something change in the syntax since the book appeared? (the other form compiles and runs fine)

Share this post


Link to post
Share on other sites

I do not know. This is the complete file,  header inserted.

#include <systemc>
SC_MODULE(NAME) {
SC_CTOR(NAME);
};
SC_HAS_PROCESS(NAME);
NAME::NAME(sc_module_name nm)
: sc_module(nm)
{}

And the error message

NAME.cpp:6:11: error: expected constructor, destructor, or type conversion before '(' token
NAME::NAME(sc_module_name nm)
^
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

 

Share this post


Link to post
Share on other sites

Hello @katang,

The error spawns from not specifying the namespace resolution correctly.

Following code compiles without  errors:

NAME.h:

#ifndef NAME_H_
#define NAME_H_
#include <systemc>
SC_MODULE(NAME) {
  SC_CTOR(NAME);
};
#endif // NAME_H_

NAME.cpp:

#include "NAME.h"

SC_HAS_PROCESS(NAME);
NAME::NAME(sc_core::sc_module_name nm) //< Added sc_core namespace resolution.
  : sc_core::sc_module(nm) //< Added sc_core namespace resolution.
{}

Regards,

Ameya Vikram Singh

Share this post


Link to post
Share on other sites

Hello @Ganesan R,

Since it is usual C++ code.

You just need to define it as in C++. For e.g.:

#include "NAME.h"

SC_HAS_PROCESS(NAME);
NAME::NAME(sc_core::sc_module_name nm) //< Added sc_core namespace resolution.
  : sc_core::sc_module(nm) //< Added sc_core namespace resolution.
{}


//////////////////////////////
// Check below
/////////////////////////////
NAME::~NAME()
{
  // You Destructor body
}

Hope it helps.

Regards,

Ameya Vikram Singh

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...