I want to make instance with array in SystemC - c++

I want to make instance with array in SystemC.
I want to write as follows:
module name = new module[10];
or
for(int i = 0; i < 10; i++){
module name[i]("any names")
}
However, I did this, the compiler said:
error: no matching function for call to 'module::module()'
Please tell me how to make an instance with array.

In SystemC, you can use sc_vector instead of a plain C array, see e.g.
https://standards.ieee.org/standard/1666-2011.html (Section 8.5)
https://complex.offis.de/documents/doc_details/29-scvector-and-the-ieee-p1666-2011-systemc-standard.html
SC_MODULE(top)
{
sc_vector<module> m; // e.g. class member
SC_CTOR(top)
: m("modules", 10) // constructor
{}
};

Related

How do I declare an array of red black trees?

When I want to initialize a red black tree I do as in the documentation.
auto rbt = redBlackTree(1,2,3,4)
but if I want to declare it globally or make an array of red black trees I don't know how to do it and the documentation is not helping. I've tried various things and I frequently get errors similar to: redBlackTree!int is used as a type Can you help me? I could do it if I knew what to put instead of auto, ie, if I knew the type of redBlackTree.
I want to declare a red black tree in global scope or declare an array for which I need to declare the type, I want to do something like this:
type rbt;
void main() {
rbt.insert(3);
}
or this:
void main{
type[2] rbt;
rbt[0].insert(1);
}
You don't need to know the type of redBlackTree. You can query for at compile-time with typeof:
alias RBTree = typeof(redBlackTree(1));
RBTree rbt = redBlackTree(1, 2, 3);
This is a common and an encouraged pattern as many functions in D return Voldemort types (types that cannot be named).
In your example the type is RedBlackTree!int. If you don't use an IDE, an easy way to discover the type is pragma(msg, typeof(<functionCall>(<args>)));.
Furthermore, I should note that declaring an array of RedBlackTree works with auto:
auto arr = [redBlackTree(1, 2), redBlackTree(3, 4)];
For more help, please feel free to post the exact code that failed.
The type (using long instead of int) is RedBlackTree!long, here are some examples. Remember you have to use new to initialize the class.
import std.stdio;
import std.container;
RedBlackTree!long rbtree;
RedBlackTree!long[2] rbarray;
RedBlackTree!long[] rbdynamicarr;
RedBlackTree!long[][] rbmat;
void main() {
rbtree.writeln;
rbtree = new RedBlackTree!long;
rbtree.insert(3);
rbtree.writeln;
rbarray.writeln;
rbarray = new RedBlackTree!long[2];
rbarray.writeln;
rbdynamicarr.writeln;
int n = 3;
rbdynamicarr = new RedBlackTree!long[n];
rbdynamicarr.writeln;
rbmat.writeln;
int m = 2;
rbmat = new RedBlackTree!long[][](n,m);
rbmat.writeln;
alias RBTree = typeof(redBlackTree!long(1L));
RBTree rbalias;
rbalias = new RBTree;
rbalias.writeln;
RBTree[3] crayola;
crayola.writeln;
typeid(redBlackTree(1)).writeln;
RedBlackTree!(long, "a < b", false) hola;
hola = new RedBlackTree!(long, "a < b", false);
hola.writeln;
}

no viable overloaded '=' error, what may cause this?

Note: I have uploaded my code here: https://onlinegdb.com/r1P8APG0I you can run it and reproduce the bug.
I wrote an abstract Character class with is inherited by many classes like Soldier and Medic. also, I have a Game class which has:
mtm::Dimensions dimensions;
std::vector<std::shared_ptr<Character>> board;
Important Note: I can't change the code that's written above, so please consider that.
I need to write a copy c'tor for this class Which makes a copy that isn't related at all to the previous one after the call is finished, So I wrote:
Game::Game(const Game &other):dimensions(other.dimensions){
int board_size = dimensions.getRow() * dimensions.getCol();
for (int i = 0; i < board_size; ++i) {
Character* copy = other.board[i]->clone();
board[i]=*copy;
}
}
But I'm getting an error saying:
error: no viable overloaded '=' board[i]=*copy;
What does this mean and how may I fix it?
You may need the following implementation of clone over Medic class which inherits Character
Character * Medic::clone() const {
return new Medic(*this);
}
Update: I tried writing =copy instead of =*copy but got exactly the same error.
You are dereferencing a pointer here
board[i]=*copy;
If instead you are trying to construct a std::shared_ptr then create one using your raw pointer
board[i] = std::shared_ptr<Character>(copy);
which more concisely is just
board[i] = std::shared_ptr<Character>(other.board[i]->clone());

NodeJS Addon Unordered_map not supported?

I am working with the Laurena library for C++ to add serialization to JSON to my Node Addon. When I initialize the library, it gets to a particular point in the code where it defines two unordered_map objects. They aren't initialized, but instead immediately used (as in the code below). ANY access to any data or any methods within the unordered_maps causes a vector subscript out of range failure.
But ONLY in nodejs.
If I pull the addon code and dump it into a Visual Studio 2013 C++ Console application, without ANY changes, it runs perfectly. Can anyone point me in the direction of what it is about these unodered_maps that isn't supported in node that is in a regular console app?
using namespace laurena;
std::unordered_map<std::string, const descriptor*> classes::_classes_by_name;
std::unordered_map<size_t, const descriptor*> classes::_classes_by_typeid;
void classes::add(const descriptor* myClass)
{
for(int i = 0; i< _classes_by_typeid.size(); i++)
{
printf("in array I (%d) : %Iu", i, _classes_by_typeid[i]); //FAILS!
}
// also failes
printf("Access ANYTHING? %s \n", _classes_by_typeid.hash_function());
// Doesn't fail? WTF??
printf("Post Set array size :: %d\n", _classes_by_name.size());
printf("Post Set array size :: %d\n", _classes_by_typeid.size());
}
I'm Laurena's author.
Laurena's library in this current version has a big flaw as it use global static variables to store classes descriptors. A better implementation would have been to store them into a singleton initialized dynamically.
A possible explain is you call laurena::classes::add from another library's static member/global data constructor. Static / global datas constructors are executed before int main (...)
In this case, if your data's constructor is called before laurena's static maps constructors, then yes you can have the error you describe. See
What’s the “static initialization order fiasco”? at https://isocpp.org/wiki/faq/ctors#static-init-order for more details about this problem.
Then there is two options:
1) laurena::classes static datas must be wrapped into a singleton dynamically created.
laurena::classes::add method should looks then
void classes::add(const descriptor* myClass) // classes::add is a static class function
{
classes* p = classes::get_or_create_instance ();
p->_classes_by_name[myClass->name()] = myClass;
p->_classes_by_typeid[std::type_index(myClass->type()).hash_code()] = myClass;
}
2) Move calls to classes::add into int main ( ... ) :
int main ()
{
// laurena's initialization
laurena::classes::init();
// let's declare TheNerd serializables classes :
declare_TheNerd_classes();
...
}
If you can't use option 2, option 1 is something i could fix.

How to pass argument to function - dealing with reference

I have some problems with C++.
I build application using GAlib library (it's a C++ Library of Genetic Algorithm Components - http://lancet.mit.edu/ga/).
In one of examples (full code of example: http://lancet.mit.edu/galib-2.4/examples/ex6.C) author create function that initializes tree:
void TreeInitializer(GAGenome & c)
{
GATreeGenome<int> &child=(GATreeGenome<int> &)c;
// destroy any pre-existing tree
child.root();
child.destroy();
// Create a new tree with depth of 'depth' and each eldest node containing
// 'n' children (the other siblings have none).
int depth=2, n=2, count=0;
child.insert(count++,GATreeBASE::ROOT);
for(int i=0; i<depth; i++){
child.eldest();
child.insert(count++);
for(int j=0; j<n; j++)
child.insert(count++,GATreeBASE::AFTER);
}
}
He invoke function in that way:
genome.initializer(TreeInitializer);
and He don't pass an argument. When I trying pass argument to that function by changing declaration for example:
void TreeInitializer(GAGenome &, int deph);
Compiler shows me errors. I don't know how to invoke this function properly. I know it's related to reference but passing (or not passing) argument in that way is new to me.
How to pass more arguments to that function?
The line genome.initializer(TreeInitializer); does not call TreeInitializer - instead, it passes the pointer to that function to genome.initializer, so it can call it with whatever arguments it needs/multiple times. If you want TreeInitializer to accept more arguments, you need to modify the initializer to accept a different type of function. It is probably defined as
void initializer(void (*f)(GAGenome&))
{
// ...
f(genome);
// ...
}
You need to change the argument type to void (*f)(GAGenome&, int) and change the lines that call f.
On the other hand, if you can't modify the initializer function but want to be able to specify the depth, then the best you can do is make a global variable that you set to whatever depth you need and have TreeInitializer use that variable. It isn't a clean solution if you think global variables are evil, but it's your only choice if the initializer doesn't let you supply any "context".
What you what to do in
GATreeGenome<int> &child=(GATreeGenome<int> &)c;
is a casting from a GAGenome to GATreeGenome . If you haven't a casting operator for this case, it won't work...

Referencing members of an object within a class type vector

Ok guys this problem has been bugging me all day and I cant seem find a solution. I know Its a long post but I would be very grateful for any help you can offer.
I am working on a chatbot program that reads in from a .dat file to populate a library of keywords. Taking an object orientated approach I defined a class called "Keyword", the class definition is shown below:
class Keyword
{
public:
//_Word holds keyword
vector<string> _Word;
//_Resp holds strings of responses
vector<string> _Resp;
//_Antymn holds words that are the opposite to keyword
vector<string> _Antymn;
// Constructor
Keyword()
{
// Clears members when new instance created
_Word.clear();
_Resp.clear();
_Antymn.clear();
}
};
Therefore every time a new keyword is found in the .dat file, a new instance of the class keyword must be created. To store all these instances of keyword I create another vector but this time of type Keyword and call it library:
typedef vector<Keyword> Lib;
Lib library;// this is the same as saying vector<Keyword> library
Now this is the problem I have: After a user inputs a string I need to check if that string contains a keyword from the library i.e. I need to see if the string in _Word appears in the user input. Looking at it from a hierarchy of vectors you have:
The top level --> libary //*starting point
--> Keyword
--> _Word
-->"A single string" <-- I want to reference this one
--> _Resp
-->"Many strings"
--> _Antymn
-->"Many strings"
Phew! I hope that made sense.
This is the code I started to write:
size_t User::findKeyword(Lib *Library)
{
size_t found;
int count = 0;
for(count = 0; count<Library->size(); count++)
{
found = _Input.find(Library->at(count)); // this line needs to reference _Word from each keyword instance within library
if(found!= string.npos)
return found;
}
return 0;
}
I have also tried to use the "operator[]" method but that doesnt seem to do what I want either.
Does anyone have any idea ? I would be very suprised if it couldn't be done. Thank you in advance.
A bunch of issues first:
identifiers beginning with an underscore followed by a capital
letter are reserved in any namespace
the clear() call in the Keyword constructor are pointless and possibly
harmful to optimization
Why is word_ a vector? I though it is one keyword.
struct Keyword
{
// real words as identifiers, no underscores
//anywhere if they are public
std::string word;
std::vector<std::string> respones;
std::vector<std::string> antonym;
};
typedef std::vector<Keyword> Lib;
/// finding a keyword
#include <algorithm>
Lib::iterator findKeyword(const Lib& l, const std::string& x) {
return std::find_if(begin(l), end(l),
[](const Keyword& kw) { return kw.word == x; })
// if stuck on non C++11 compiler use a Functor
}
You have to change your code to this:
for(count = 0; count<Library->size(); count++)
{
for(int j = 0; j < Library->at(count)._Word.size(); ++j){
found = _Input.find(Library->at(count)._Word[j]);
^^^^^^^^^
if(found!= string.npos)
return found;
}
}
in order to access the member variable and to iterate through your vector of strings. Library->at(count) is an object of class the Keyword.
I assume that _Input.find() takes a string as argument.
If your Keyword instance stores just one keyword, you might as well change it to string _Word, so that you wold not need the second loop.
for(count = 0; count<Library->size(); count++)
{
found = _Input.find(Library->at(count)._Word);
if(found!= string.npos)
return found;
}
And to enforce the other comments: you should not use the preliminary _-underscore in your variable names since they are reserved by the implementation.