I am trying to run this code but it is throwing an error. Can't figure it how to resolve it. I am new to structure and I was trying to make a link list program.
The error:
S_linklist1.cpp: In member function ‘linklist::NODE linklist::create_node(int)’: S_linklist1.cpp:23:11:
error: could not convert ‘temp’ from ‘linklist::NODE*’ to ‘linklist::NODE’ return temp;
S_linklist1.cpp: In member function ‘void linklist::add(int)’: S_linklist1.cpp:35:7:
error: cannot convert ‘linklist::NODE’ to ‘linklist::NODE*’ in assignment temp1=create_node(data);
The program:
#include<iostream>
#include<cstdlib>
using namespace std;
class linklist{
private: struct NODE {
int data;
struct NODE* link;
};
struct NODE* START=NULL;
NODE create_node(int s_data)
{
struct NODE* temp;
temp=new NODE;
temp->data=s_data;
temp->link=NULL;
return temp;
}
public : void add(int);
};
void linklist :: add(int data)
{
struct NODE* temp1;
temp1=new NODE;
temp1=create_node(data);
cout<<"Return success"<<"\n";
}
int main()
{
linklist obj;
obj.add(10);
}
Please help me with this. Thanks
Changing
NODE create_node(int s_data)
To
NODE *create_node(int s_data)
it is compiled on g++4.8.4.
BTW 'struct NODE' can be simply 'NODE' in C++.
Related
The problem:
I have a pointer to struct file called flip, an int called cmd and an unsigned long called arg.
The private_data field in struct file points
the private_data structure is defined as follows:
typedef struct {
unsigned char key;
int read_state;
} my_private_data;
and ioctl function is defined as follows:
int my_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case CMD_CHANGE_KEY:
filp->private_data->key = (char)cmd;
filp->private_data->read_state = (int)arg;
break;
case CMD_SET_READ_STATE:
filp->private_data->key = (char)cmd;
filp->private_data->read_state = (int)arg;
break;
case CMD_ZERO:
kfree(buff_caesar);
break;
}
return 0;
}
However, when I try to compile the code, I get the following warning/error:
Warning: dereferencing 'void *' pointer.
Requesting for Member 'key' in something not a struct or union.
Warning: dereferencing 'void *' pointer.
Requesting for Member 'read_state' in something not a struct or union.
What can I do to fix this?
Here's the open method:
int my_open(struct inode *inode, struct file *filp)
{
int minor = MINOR(inode->i_rdev);
if(minor == ONE) {
/* Caesar*/
filp->f_op = &fops_caesar;
}
else if(minor == TWO){
/*Xor*/
filp->f_op = &fops_xor;
}
else return -ENODEV;
my_private_data* privateData = NULL;
privateData = kmalloc(sizeof(my_private_date),GFP_KERNEL);
if (privateData==NULL){
return -1;
}
filp->private_data = privateData;
return 0;
}
The struct file member .private_data is defined as a void *, so filp->private_data itself has no members. If you want to store data items to your structure, you could access them with a local of that type; eg, within my_ioctl() :
my_private_data *info;
. . .
if (filp->private_data == NULL) {
error handling
}
info = filp->private_data;
. . .
info->key = (char) cmd;
info->read_state = (int) arg;
I am using Rcpp to create a package in R that leverages C++ code. I have read all of the Rcpp vignettes, but I haven't been able to find a solution to the following problem.
One of the C++ classes I'm trying to use contains a pointer. I am exposing the class using a module. When I try to install the package in R, I get the following error.
error: expected unqualified-id before '*' token.field("*w", &ffm_model::*w)
What am I doing wrong?
Code for Class Containing Pointer
typedef float ffm_float;
typedef int ffm_int;
class ffm_model {
public:
ffm_int n; // number of features
ffm_int m; // number of fields
ffm_int k; // number of latent factors
ffm_float *w = nullptr;
bool normalization;
~ffm_model();
};
Code for Corresponding RCPP Module
RCPP_MODULE(ffmModelMod){
using namespace Rcpp;
//Expose class as ffm_model on the r side
class_<ffm_model>( "ffm_model")
.field("n", &ffm_model::n)
.field("m", &ffm_model::m)
.field("k", &ffm_model::k)
.field("*w", &ffm_model::*w)
.field("normalization", &ffm_model::normalization)
.method("~ffm_model",&ffm_model::~ffm_model)
;
}
I had a similar problem, and as Dirk mentions, it is due to types that cannot be mapped automatically, such as float*.
The following workaround works for me:
Do not expose the fields with problematic types to R.
Instead, expose get() and set() functions to the fields above.
Here is an example, in which both the (unproblematic) value field and the (problematic) child field (a pointer to an object of the same class) are hidden:
Class
#include <Rcpp.h>
using namespace Rcpp;
class node
{
public:
double value; // Voluntarily hidden from R
node* child; // Must be hidden from R
// Exposed functions
void setVal(double value);
double getVal();
node* createNode(double value); // return pointer to a node
node* createChild(double value); // set child
node* getChild();
};
Methods
void node::setVal(double value){
this->value = value;
}
double node::getVal(){
return this->value;
}
node* node::createNode(double value){
node* n = new node;
n->value = value;
return n;
}
node* node::createChild(double value){
this->child = createNode(value);
return child;
}
node* node::getChild(){
return this->child;
}
RCPP Module
RCPP_MODULE(gbtree_module){
using namespace Rcpp;
class_<node>("node")
.constructor()
.method("setVal", &node::setVal)
.method("getVal", &node::getVal)
.method("createNode", &node::createNode)
.method("createChild", &node::createChild)
.method("getChild", &node::getChild)
;
}
Usage in R
n <- new(node)
n$setVal(2)
n$getVal()
n2 <- n$createNode(1) # unrelated node
n3 <- n$createChild(3) #child node
n$getChild() #pointer to child node
n3
I am attempting to create template classes where each can solve a specific facet of the problem so to be able to mishmash them without resorting to creating the traditional abstract virtual classes.
For that, I believe CRTP would be the best paradigm.
However, when using CRTP a bit more I found trapped on this weak resolution logic - compiler (g++ 4.8.2) cannot distinguish between two methods on different classes even though their signature is different - only the method name is the same.
The classes implementation:
template< class T >
class A {
public:
void foo( uint32_t val ) {
T* me = static_cast<T*>( this );
me->doit();
}
};
template< class T >
class B {
public:
void foo() {
uint32_t v32 = 10;
T* me = static_cast<T*>( this );
me->foo( v32 );
}
};
class Derived : public A<Derived>,
public B<Derived>
{
public:
void doit() {
std::cout << "here" << std::endl;
}
};
Then it is used as
Derived d;
d.foo();
When compiled, this error surfaces:
$ g++ -std=c++11 -c testLambda.cpp
testLambda.cpp: In function ‘int main(int, char**)’:
testLambda.cpp:102:7: error: request for member ‘foo’ is ambiguous
d.foo();
^
testLambda.cpp:25:10: note: candidates are: void B<T>::foo() [with T = Derived]
void foo() {
^
testLambda.cpp:16:10: note: void A<T>::foo(uint32_t) [with T = Derived; uint32_t = unsigned int]
void foo( uint32_t val ) {
Is this a compiler bug or the actual expected result?
User pubby8 at reddit.com/r/cpp responded (quote) a quick fix is to add this to Derived's class body:
using A<Derived>::foo;
using B<Derived>::foo;
I am trying to store a pointer to a member function in a structure which will be used to call the function later in my program.
Something like this:
// abc.h
namespace XYZ {
typedef void func(const uint8_t *buf, int len);
struct holder
{
// other members
func * storePtr;
}
} // end of namespace
the other file as:
// pqr.h
#include abc.h
namespace XYZ {
class pqr {
// data members and other functions
void func1(const uint8_t *buffer, int length);
void func2(func *section);
void func3();
}
} // end of namespace
Now my cpp file needs to store instance of this func1 in my structure member storePtr
// app.cpp
#include pqr.h
void pqr::funct3()
{
// Do something
func2(func1);
}
void pqr::func2(func * section)
{
holder h;
h.storePtr = section;
}
But I am getting compilation error at line "func2(func1);" as
"error C3867: 'pqr::func1': function call missing argument list; use '&pqr::func1' to create a pointer to member"
I have used &pqr:: to define the scope but it also doesn't solve my problem and I am not able to understand what to do.
Pointers to member function are not the same thing as pointers to normal functions - have a look at the explanation and example here: http://msdn.microsoft.com/en-us/library/k8336763.aspx
I have the following code:
#include "iostream"
#include "conio.h"
using namespace std;
class Student {
private:
int no;
public:
Student(){}
int getNo() {
return this->no;
}
friend istream& operator>>(istream& is, Student& s);
friend ostream& operator<<(ostream& os, const Student& s);
};
ostream& operator<<(ostream& os, const Student& s){
os << s.getNo(); // Error here
return os;
}
int main()
{
Student st;
cin >> st;
cout << st;
getch();
return 0;
}
When compiling this code, the compiler produced the error message: "error C2662: 'Student::getNo' : cannot convert 'this' pointer from 'const Student' to 'Student &'"
But if I made the no variable public and change the error line like: os << s.no; then things worked perfectly.
I do not understand why this happened.
Can anyone give me an explanation, please?
Thanks.
Because s is const in that method, but Student::getNo() isn't a const method. It needs to be const.
This is done by changing your code as follows:
int getNo() const {
return this->no;
}
The const in this position means that this entire method does not change the contents of this when it is called.