expression must be a modifiable lvalue (for array) [duplicate] - c++

This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 5 years ago.
I have this program I'm working on, and one part of it is a linked list which I am working on making. I got this far, but at this point in the code it tells me (on the 3rd to last line) that inst must be a modifiable lvalue. I'm not sure what I'm doing wrong here.
#include <iostream>
using namespace std;
struct node{
float color[3];
float v[2*3];
node *next;
};
class TriangleList {
private: node *head;
private: node * tail;
public:
TriangleList() {
head = NULL;
tail = NULL;
}
void add(float vertices[], float colors[]) {
node *inst = new node;
inst->v = vertices;
}
};

Below statement is not correct, as you can't do inst->v = vertices because by doing this you are trying to change the base address of v, read the error properly.
error: incompatible types in assignment of ‘float’ to ‘float [6]’*
inst->v = vertices;
you may want to do like below
for(int i=0;i<len;i++) { /* len is the length of vertices */
inst->v[i] = vertices[i];
}

Related

How do you write a function that returns a node which value corresponds to a value stored in a variable?

I stumbled across this question in an old textbook I bought ages ago, whilst strictly speaking it is not too difficult, I could not find a post here that simply answered this one question. As such I thought "Hey perhaps someone starting out might be confused about this", and so I put together the following code:
#include <iostream>
using namespace std;
// Structures
struct charNode {
char Node;
charNode *next;
};
// Functions
charNode* getCharNode(char c) {
return ; //<----- Return Node Here
}
What this needs is to be put in a class or else you must have a global variable that points to the head of that singly linked list.
An example class could look like this:
#include <iostream>
class singly_linked_list {
struct charNode {
char Node;
charNode *next;
};
public:
// find the charNode with the value `c`:
charNode* getCharNode(char c) {
for(charNode* curr = head; curr != nullptr; curr = curr->next) {
if(curr->Node == c) return curr;
}
return nullptr;
}
// add member functions to add/remove charNode's from the list etc
// and implement one of "the rule of 3" or "the rule of 5"
private:
charNode* head = nullptr;
};
You can implement getCharNode() function like in following code. I used this function for an example of creating singly linked list of chars. Also created extra function print_list() which outputs linked list to console.
Notice that I did only allocation of nodes (new operator), and didn't do deallocation (delete), I left this task for you to do, if you care about memory leaks.
Try it online!
#include <iostream>
// Structures
struct charNode {
charNode(char value, charNode * _next = nullptr)
: Node(value), next(_next) {}
char Node;
charNode *next;
};
// Functions
charNode* getCharNode(char c, charNode * next = nullptr) {
return new charNode(c, next);
}
void print_list(charNode const * node) {
if (!node)
return;
std::cout << node->Node << " ";
print_list(node->next);
}
int main() {
charNode * list = getCharNode('a',
getCharNode('b', getCharNode('c')));
print_list(list);
}
Output:
a b c

what is "struct* struct newStruct(int somedata) { } " syntax in c++ [duplicate]

This question already has answers here:
When to use an elaborated type specifier
(5 answers)
Closed 2 years ago.
I was trying to make a tree in c++ and came across this code which is really confusing me.
struct node
{
int data;
struct node* left;
struct node* right;
};
// The code bellow this is the part i dont understand
struct node* newNode(int idata)
{
node* node = new struct node;
node->data = idata;
node->left = NULL;
node->right = NULL;
return node;
}
what is struct node* ? Some kind of a structure, but a pointer? Also shouldn't structs have ; at the end? For example, node has ; at the end of the body but not node* ?
struct node* is the return type of function newNode() and not a definition in itself. The line you are referring to is the signature of the function newNode. It is followed by the function definition between the two curly braces {}. A function definition does not need a ; at the end.

Why I am getting different outputs with these 2 assignments, one in a function and other in main of a pointer? [duplicate]

This question already has answers here:
Function does not change passed pointer C++
(4 answers)
Closed 3 years ago.
"I was trying to assign NULL to a pointer root in a function. But it doesn't really get assigned by NULL using function. But when I tried to assign root = NULL in main it get assigned. I am not getting why it happens so?
#include<bits/stdc++.h>
using namespace std;
struct node{
int key;
};
void deletion(struct node* root){
root=NULL;
}
void print(struct node* temp){
if(!temp)
return;
cout<<temp->key;
}
int main(){
struct node* root = new struct node;
root->key=10;
cout<<"Initially : ";
print(root);
deletion(root);
cout<<"\nAfter deletion() : ";
print(root);
root=NULL;
cout<<"\nAfter assigning in main() : ";
print(root);
}
The output I am getting is :
Initially : 10
After deletion() : 10
After assigning in main() :
You're passing the pointer by value and modifying that value. To modify the value of a variable being passed, you can either pass it by reference (C++-style) or pass it by pointer(C-style). With C-style, remember to dereference the pointer to change its value for the caller.
C++-style:
void foo(const struct node*& n);
C-style:
void foo(const struct node** n);

Simple PUSH function for a C++ stack [duplicate]

This question already has answers here:
Function does not change passed pointer C++
(4 answers)
Closed 5 years ago.
I am trying to implement a simple stack in C++ using a linked list, but I am completely stumped by the PUSH() function. I've been working on it all night and it's nearly driven me bonkers. It should insert an element at the top of the stack, but every way I've tried to implement is has had issues. An excerpt of my relevant code is as follows:
template <typename T>
struct NODE{
T data;
NODE<T> *next;
}
template <typename T>
void PUSH(T x, NODE<T> *S){
NODE<T> *tmp = new NODE<T>;
tmp->data = x;
tmp->next = S;
S = tmp;
}
int main(){
NODE<int> *test = new NODE<int>;
test->data = 111;
test->next = NULL;
PUSH(99, test);
PUSH(88, test);
std::cout << test->data << std::endl;
}
I would expect the last line to print 88, but instead it prints 111. When I try to access the next element, I get a segfault so clearly I must be doing something wrong. Maybe I'm just tired, but hopefully one of you could shine some light on where I'm messing up, it seems correct to me.
void PUSH(T x, NODE<T> *S)
{
}
You are passing S by value, so any changes you make to S inside PUSH will not be visible outside.So even though you are adding elements to your stack, but your top remains the first node (111).
You can either pass a pointer to pointer to S or a reference to S
void PUSH(T x, NODE<T> **S)
{
NODE<T> *tmp = new NODE<T>;
tmp->data = x;
tmp->next = *S;
*S = tmp;
}

C++ adding item onto a linked list [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
I'm trying to add a node onto the beginning of a linked list (push function). I'm getting 2 errors:
1) invalid conversion from 'Node int*' to 'int' (points to test.Push(&test2); in main())
2) initializing argument 1 of 'int Linked_List::Push(ItemType) [with ItemType = int]' (points to function push)
I'm really not sure what the problem is. If I remove the & out of test.Push(&test2); in main() then I get a lot more errors, so I assume it's correct.
//.h
#ifndef Linked_List_h
#define Linked_List_h
template <typename ItemType>
class Node
{
public:
ItemType Data;
Node <ItemType> *next;
};
template <typename ItemType>
class Linked_List
{
public:
Node <ItemType> *start;
Linked_List();
int Push(ItemType newitem);
};
#endif
.
//.cpp
#include "Linked_List.h"
template <typename ItemType>
Linked_List <ItemType>::Linked_List(){
start = NULL;
}
template <typename ItemType>
int Linked_List <ItemType>::Push(const ItemType newitem){ //error
Node <ItemType> *nnode; //create new node to store new item
nnode -> next = start -> next; //new item now points previous first item of list
start -> next = nnode; //'start' pointer now points to the new first item of list
return 1;
}
int main(){
Linked_List <int> test;
Node <int> test2;
test2.Data = 4;
test.Push(&test2); //error
}
Your function's signature expects a ItemType, which is int in your case:
int Push(ItemType newitem);
But you are trying to pass a Node<ItemType>, hence you get an error.
Your Push function is already creating a a node internally, so you'd pass the integer directly into it:
Linked_List <int> test;
int test2 = 4;
test.Push(test2);
I need to point out that your code has several other problems besides that, though - for starters, this snippet:
Node <ItemType> *nnode; //create new node to store new item
Does not create a Node - it just declares a pointer.
I'd strongly advise you to read up on C++'s basics.
Push takes the template type, so int in this case. What you want to do is something like:
Linked_List<int> test;
test.push(4);