Jump to content
katang

How to define a FIFO of custom structure

Recommended Posts

I want to define a FIFO of user-defined structures. The program below presents with message:

no match for 'operator <<'

If I change <Transfer> to <int>, int compiles fine.
 

#ifndef scMWE_h
#define scMWE_h
#include <systemc>
#include <iostream>
typedef struct {
    int Select;
    int Content;
} Transfer, *TransferPtr;

SC_MODULE(scMWE) {
    scMWE(sc_core::sc_module_name nm);
    sc_core::sc_fifo<Transfer> from;
};
#endif


#include "scMWE.h"

SC_HAS_PROCESS(scMWE);
scMWE::scMWE(sc_core::sc_module_name nm)
: sc_core::sc_module(nm)
{}

 

Share this post


Link to post
Share on other sites

The type you pass as a template argument to sc_core::sc_fifo<T> has to fulfill a couple of rules, which are described in detail in clause 6.23.3 of the IEEE Std 1666-2011. In summary, your type Transfer needs to provide:

a) The output stream operator:

std::ostream& operator<<(std::ostream&, const Transfer&);

This one is missing, which is hinted to you by the compiler error.

b) The assignment operator (not missing as auto-generated by the compiler for a struct):

const Transfer& operator=(const Transfer&);

c) The default constructor if any other constructor is defined for your type Transfer (not necessary for a pure struct as in your code snippet):

Transfer::Transfer();

 

Share this post


Link to post
Share on other sites

 

In my enhanced version, the compiler said the operator must take exactly one argument. I really cannot find out why.

#ifndef scMWE_h
#define scMWE_h
#include <systemc>
#include <iostream>

struct Transfer_Type
{
    int Select;
    int Content;
    std::ostream& operator << (std::ostream& os, const Transfer_Type& I)
    {
      return os;
    }

};

SC_MODULE(scMWE) {
    scMWE(sc_core::sc_module_name nm);
    sc_core::sc_fifo<Transfer_Type> from;
};
#endif

 

Share this post


Link to post
Share on other sites

Hello @katang,

You need to declare the method as a friend method and the definition has to be moved to corresponding source file.

For e.g.:

// scMWE.h
#ifndef scMWE_h
#define scMWE_h
#include <systemc>
#include <iostream>

struct Transfer_Type
{
    int Select;
    int Content;
    friend std::ostream& operator << (std::ostream& os, const Transfer_Type& I); //< Declaration for the friend method.
};

SC_MODULE(scMWE) {
    scMWE(sc_core::sc_module_name nm);
    sc_core::sc_fifo<Transfer_Type> from;
};
#endif

scMwe.cpp:

// scMWE.cpp
#include "scMWE.h"

// Definition for the friend method.
std::ostream& operator << (std::ostream& os, const Transfer_Type& I)
{
  return os;
}

Hope it helps.

Regards,

Ameya Vikram Singh

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

×