Sign in to follow this  
Followers 0
katang

How to define constructor in cpp file?

5 posts in this topic

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

Hello @katang,

It seems the macro NAME has not been defined.

From the book they have mentioned about the file "NAME.h". There recommendation on providing simpler and cleaner declarations.

Regards,

Ameya Vikram Singh

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

swami-cst likes this

Share this post


Link to post
Share on other sites

Thanks a lot. In my eyes the g++ error message is misleading. I was looking also for the error in defining macro NAME.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0