I've got some trouble in allocating (::operator new) the declared structs inside an class, at its method, as getting an non-nub-readable error:
error: cannot convert 'Automata::state*' to 'state*' in assignment
\
I have tried removing the "Automata::" declaration, placing "this->" and other random things, no success. Follows an example code;
#include <iostream>
#include <new>
class Automata{
public:
struct quintuple{
struct state *stateStr = NULL;
int stateSize = 0;
};
struct state{
struct symbol *symbolStr = NULL;
};
struct symbol{
char *state = NULL;
int stateSize = 0;
};
struct quintuple quintupleStr;
//Functions
void quintuple(int stateValidCounter);
};
void Automata::quintuple(int stateValidCounter){
this->quintupleStr.stateStr = (struct Automata::state *) ::operator new(sizeof(struct Automata::state) * stateValidCounter);
return;
}
int main(){
Automata automata;
automata.quintuple(5);
return 0;
}
/*
g++ -std=c++11 example.cpp -o example.out
example.cpp: In member function 'void Automata::quintuple(int)':
example.cpp:23:30: error: cannot convert 'Automata::state*' to 'state*' in assignment
this->quintupleStr.stateStr = (struct Automata::state *) ::operator new(sizeof(struct Automata::state) * stateValidCounter);
^
*/
Thank you for the attention.
Well, to be honest this is an sad tiredness event.
I would like to accept the #Passer By if he answers at.
The answer is simple as #Passer By comment at main section.
Be the code
#include <iostream>
#include <new>
class Automata{
public:
struct quintuple{
struct state *stateStr = NULL;
int stateSize = 0;
};
struct state{....};
struct quintuple quintupleStr;
//Functions
void quintuple(int stateValidCounter);
};
void Automata::quintuple(int stateValidCounter){
this->quintupleStr.stateStr = (struct Automata::state *) ::operator new(sizeof(struct Automata::state) * stateValidCounter);
return;
}
int main(){
Automata automata;
automata.quintuple(5);
return 0;
}
simply switch the struct code position..
class Automata{
public:
struct state{....};
struct quintuple{
struct state *stateStr = NULL;
int stateSize = 0;
};
struct quintuple quintupleStr;
....;
};
I still think the compiler error is a little misleading
Related
This question already has answers here:
error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
(3 answers)
Closed 2 years ago.
Here is my C code, I use void InitList(List &L); However Code::Blocks`s buildlog has an error:
expected ';', ',' or ')' before '&' token
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 10
typedef int ElementType;
struct SeqList;
typedef struct SeqList List;
void InitList(List &L);
struct SeqList
{
ElementType *data;
int CurLength;
};
/*----------------------------------*/
void InitList(List &L)
{
(&L)->data = malloc(sizeof(ElementType)*MaxSize);
(&L)->CurLength = 0;
}
int main()
{
List L;
InitList(&L);
return 0;
}
But I tried in C++, there is no error:
#include <iostream>
using namespace std;
#define InitSize 100
typedef int ElementType;
struct SeqList;
typedef struct SeqList List;
void InitList(List &L);
struct SeqList
{
ElementType *data;
int CurLength;
};
/*----------------------------------*/
void InitList(List &L)
{
L.data = new ElementType[InitSize]; //L.data = malloc(...)
L.CurLength = 0;
}
int main()
{
List L;
InitList(L);
return 0;
}
You can't pass that in a function declaration in C. You need to be using * if you want to reference the pointer to that data.
void InitList(List *L)
{
L->data = malloc(sizeof(ElementType)*MaxSize);
L->CurLength = 0;
}
And then
List *L;
InitList (&L);
In void InitList(List &L);, L is being passed by reference. C does not support references, that is a C++ feature. In C, you need to pass L by pointer instead (which is exactly what your main() is trying to do when using List L; InitList(&L);, but the declaration of InitList() is wrong for that), eg:
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 10
typedef int ElementType;
struct SeqList;
typedef struct SeqList List;
void InitList(List *L);
void CleanupList(List *L);
struct SeqList
{
ElementType *data;
int CurLength;
};
/*----------------------------------*/
void InitList(List *L)
{
L->data = malloc(sizeof(ElementType)*MaxSize);
L->CurLength = 0;
}
void CleanupList(List *L)
{
free(L->data);
L->CurLength = 0;
}
int main()
{
List L;
InitList(&L);
...
CleanupList(&L);
return 0;
}
Given the following snippet:
/* trie.h file */
using namespace std;
#include <list>
typedef struct tn {
char ch;
list<struct tn*> ptrs;
} TrieNode;
class Trie {
public:
static const TrieNode* EMPTY;
//... other member functions
};
/* trie.cpp file */
#include "trie.h"
// declare, define static variables of the Trie class
TrieNode* Trie::EMPTY = (TrieNode*) malloc( sizeof(TrieNode) ); // <-- seems to work fine
// the statements below seem to yield errors
Trie::EMPTY->ch = '.';
Trie::EMPTY->ptrs = nullptr;
I get the error: "This declaration has no storage type or type specifier" if I try to instantiate the struct member variables of the static constant variable EMPTY. I know storing EMPTYas a struct object rather than a pointer to the struct object would be easier but was curious how this would work. Thanks.
You can't put statements like Trie::EMPTY->ch = '.'; and Trie::EMPTY->ptrs = nullptr; in global scope, they can only be executed inside of functions, constructors, etc.
Try something more like this instead:
/* trie.h file */
#include <list>
struct TrieNode {
char ch;
std::list<TrieNode*> ptrs;
};
class Trie {
public:
static const TrieNode* EMPTY;
//... other member functions
};
/* trie.cpp file */
#include "trie.h"
// declare, define static variables of the Trie class
static const TrieNode emptyNode{'.'};
const TrieNode* Trie::EMPTY = &emptyNode;
Live Demo
In a namespace (outside any function) you may place only declarations.
Statements like these:
Trie::EMPTY->ch = '.';
Trie::EMPTY->ptrs = nullptr;
are not allowed to be placed in a namespace, because they are not declarations.
Moreover, this statement:
Trie::EMPTY->ptrs = nullptr;
does not make sense, because the object ptrs is not a pointer, and a std::list cannot be initialized from nullptr.
Pay attention to that instead of the C function malloc(), you should use the C++ operator new.
This definition:
TrieNode* Trie::EMPTY = (TrieNode*) malloc( sizeof(TrieNode) );
is also incorrect, because you forgot to specify the qualifier const.
It should be rewritten like this:
const TrieNode* Trie::EMPTY = new TrieNode { '.' };
Here is a demonstrative program
#include <iostream>
#include <list>
typedef struct tn {
char ch;
std::list<struct tn*> ptrs;
} TrieNode;
class Trie {
public:
static const TrieNode* EMPTY;
//... other member functions
};
// the definition below must be placed in a cpp module
// it presents here only for demonstration.
const TrieNode* Trie::EMPTY = new TrieNode { '.' };
int main()
{
return 0;
}
Before exiting the program you should free the allocated memory.
Instead of the raw pointer you could use the smart pointer std::unique_ptr.
I am trying to call pointer to a void * function inside the main method and the compiler is saying assigning to 'funcptr<g>' from incompatible type 'void *(void *). hello function is actually an argument to pthread_create function. That's why it is void * function. How can I create a function pointer to a void * function?
#include <iostream>
#include <pthread.h>
using namespace std;
template<typename T>
using funcptr = void (*T::*)(void *); // I think it is wrong here.
class m {
public:
template <typename T>
struct my_struct {
funcptr<T> ptr;
};
};
class g {
public:
static void *hello(void *);
};
int main() {
struct m::my_struct<g> h;
h.ptr = g::hello; // Error here
return 0;
}
How can I create a function pointer to a void * function?
hello is not a member function, but it's a static function.
So your funcptr should be as follows:
// No template needed.
using funcptr = void* (*)(void *)
Note that hello is declared with static, meaning that it's no longer a member function to g.
Static members of a class are not associated with the objects of the class.
So using void (*T::*)(void *) to cull non-member functions is incorrect.
If you're allowed to use a compiler that supports C++11, you don't even need to manually deduct its type anymore, using decltype:
// decltype deducts its exact type for you.
using funcptr = decltype(&g::hello);
class m
{
public:
struct my_struct
{
funcptr ptr;
};
};
FYI, since hello does not have its definition, you might encounter a linkage error. To prevent that, I assumed that there's some implementation inside:
static void *hello(void *)
{
// Meaningless, but..
return nullptr;
}
if you're using C++11, you can use std::function<> which just bothers about the return type and parameters of the function and not where they are defined and what are its type.
Here is the code using std::function<>
#include <iostream>
#include <functional>
#include <pthread.h>
using namespace std;
class m {
public:
template <typename T>
struct my_struct {
function<void*(void*)> ptr;
};
};
class g {
public:
static void *hello(void *) {
cout<<"Hello.."<<endl;
}
};
int main() {
struct m::my_struct<g> h;
h.ptr = g::hello;
h.ptr(nullptr);
return 0;
}
Edit:
I've found the solution by myself. I'm sorry for this question, that wasn't fully understanble for You.
Okey, I have so much code in my program. I'm writing a tree with using a list,that I'm going to write. I can't use STL library.
30_Contaner.h:
#pragma once
#include "Container_5_30.h"
#include "Tree.h"
#include "List.h"
using std::cout;
using std::cin;
class Tree : public AbstractTree
{//Cathy
protected:
struct ListStruct{
void* key;
size_t size;
ListStruct* next;
};
//Iterator *globalIterator;
class List : public AbstractList{
public:
/*some code*/
List(MemoryManager& mem) :AbstractList(mem){//constructor
}
virtual ~List(){
}
//some functions
};
struct TreeStruct{
TreeStruct *parent;
//OneLinkedList* Children = new OneLinkedList;
List *Children;//error
void *elem;
size_t size;
int childIndex;
};
int numberOfElements;
bool globalBoolForRecursion;
//TreeStruct *TreeStructInstance = new TreeStruct;
List *ListInstance = new List;//error
public:
Tree(MemoryManager& mem) :AbstractTree(mem) {
//root = new TreeStruct;//root - корень дерева (элемент структуры Дерева)
numberOfElements = 0;//кол-во элементов в дереве пока что 0
}
//some functions
};
main.cpp:
#include "30_Container.h"
#include "30_Mem.h"
#include "Tree.h"
//********************************
void main(){
Mem mem(100);
Tree tree(mem);
//some code
}
30_Mem.h
#pragma once
#include "MemoryManager.h"
// Простейший менеджер памяти, использует ::new и ::delete
class Mem : public MemoryManager
{
public:
Mem(size_t sz) :MemoryManager(sz) {}
void* allocMem(size_t sz) { return new char[sz]; }
void freeMem(void* ptr) { delete[] ptr; }
};
All methods in AbstractTree,MemoryManager and AbstractList classes are virtual. I should get instance of List class inside Tree class. But, I have a problem: I don't have default constructor for List and, of course, when I write List *list = new List; I have an error. My teacher told me that I should use pointer to memory manager of tree, or address, may be. Do you have any idea?
P.S. I need 30_Mem.h in the future. I should be writing my own new
Instead of default initialization for ListInstance add initialization of ListInstance to constructor
Tree(MemoryManager& mem) :AbstractTree(mem), ListInstance(mem) {
//root = new TreeStruct;//root - корень дерева (элемент структуры Дерева)
numberOfElements = 0;//кол-во элементов в дереве пока что 0
}
I cannot figure the syntax to declare a function pointer as a static member.
#include <iostream>
using namespace std;
class A
{
static void (*cb)(int a, char c);
};
void A::*cb = NULL;
int main()
{
}
g++ outputs the error "cannot declare pointer to `void' member". I assume I need to do something with parentheses but void A::(*cb) = NULL does not work either.
I introduced a typedef, which made it somewhat clearer in my opinion:
class A
{
typedef void (*FPTR)(int a, char c);
static FPTR cb;
};
A::FPTR A::cb = NULL;
void (*A::cb)(int a, char c) = NULL;