C++ :: Return Reference For Derived Function

Jun 3, 2014

I dont know what kind of return reference I have to put to override the following derived member function in C++:

virtual SEntityPhysicalizeParams& GetPhysicsParams() override {return ???;}

The place where I should put the return value is marked with ???. SEntityPhysicalizeParams is a struct from another header from which I dont have access to it's source file.

I tried several things but noone seemed to work out and keep getting me either error "function must return a value" or "initial value of reference to non-const must be an lValue".

Here is SEntityPhysicalize where my function is refering to:

struct SEntityPhysicalizeParams
{///////////////////////////////////////////////////////////////////
SEntityPhysicalizeParams() : type(0),density(-1),mass(-1),nSlot(-1),nFlagsOR(0),nFlagsAND(UINT_MAX),
pAttachToEntity(NULL),nAttachToPart(-1),fStiffnessScale(0), bCopyJointVelocities(false),
pParticle(NULL),pBuoyancy(NULL),pPlayerDimensions(NULL),pPlayerDynamics(NULL),
pCar(NULL),pAreaDef(NULL),nLod(0),szPropsOverride(0) {};

[Code] ....

View 8 Replies


ADVERTISEMENT

C++ :: Find Function - Return Reference To A Vector

Oct 12, 2014

Okay, so for an assignment I need to write a function called find() that returns a reference to a vector. So I have vector <int> & find(string & key); If I do this, I get the obvious warning warning: reference to local variable 'lineNum' returned [enabled by default].

If I do vector<int> & find(string & key) const; I get a huge error that starts out like

In member function 'std::vector<int>& index_table::find(std::string&) const':
indextable.cpp:74:30: error: no match for 'operator='

Am I using the const identifier incorrectly?

View 5 Replies View Related

C++ :: Return By Reference - Statement Doesn't Return Value Of N

Jan 11, 2015

From the page: [URL] ....

#include <iostream>
using namespace std;
int n;
int& test();

[Code] ....

Explanation

In program above, the return type of function test() is int&. Hence this function returns by reference. The return statement is return n; but unlike return by value. This statement doesn't return value of n, instead it returns variable n itself.

Then the variable n is assigned to the left side of code test() = 5; and value of n is displayed.

I don't quite understand the bold sentence. Shouldn't value of n and variable n be the same?

View 8 Replies View Related

C++ :: Invalid Initialization Error - Cannot Return Derived Class By Value

May 21, 2013

Code:
class Base {
public:
int base;
Base(int init=0):base(init){}
virtual ~Base(){}

[Code] .....

Invalid initialization of non-const reference of type 'Base&' from an rvalue of type 'Derived'

What does it mean, and why can't I return the Derived class by value (I'm trying to create an exact copy of Derived).

View 9 Replies View Related

C++ :: Why Assignment Operator Return By Reference

Feb 1, 2015

I have code here that uses assignment operators that doesn't return by reference and it still works. So why does my book say you need to return by reference?

Here is a quote from my book:

The return type of operator= is a reference to the invoking object, so as to allow chained
assignments a=b=c.

The code below is from my book. I simply removed '&', in the original code that has assignment operators return by reference, from IntCell & operator=. This way the assignment operator no longer returns a reference, and it still works.

#include <iostream>
using namespace std;
class IntCell {
public:
explicit IntCell( int initialValue = 0 )
{ storedValue = new int{ initialValue }; }

[Code] .....

View 3 Replies View Related

C++ :: Why Return Reference Doesn't Alter The Other Variable

Jun 1, 2014

Suppose i have a function:

int & f1(int & p) {
return p;
}
void main() {
int a,b;
b = 10;
a = f1(b);
a = 11;
cout<<b<<endl;
}

why when i change a, b doesnt change? a is supposed to be an alias of b right?

View 5 Replies View Related

C++ :: Calling Base Function From Derived Overloaded Function

Nov 10, 2014

Here is a sample of my question

class Base{
public:
int getNum();
private:
int numToGet;
}
class Derived: public Base {
public:
friend ostream& operator<<(ostream& output, const Derived &B);

[Code]...

View 1 Replies View Related

C :: Will Return Root Statement At End Ever Return Value Other Than Value Passed To Function?

Mar 29, 2013

I'm writing some functions pertaining to binary trees. I've used recursion once before while learning quicksort but am still quite new and unfamiliar with it. And this is my first time touching a binary tree. So my question: In my addnode function, will the return root statement at the end ever return a value other than the value passed to the function?

Code:

#include <stdlib.h>
struct tnode
{
int data;
struct tnode * left;
struct tnode * right;
}

[code]....

View 4 Replies View Related

C++ :: Calling A Function On Derived Class

Dec 29, 2012

I'm trying to call a function on a derived class that's in a vector of it's base class. I've made the code really simple for illustration purposes:

class Sprite {
virtual void update();
}
class Enemy : public Sprite {
virtual void update();

[Code] ....

I want to be able to just call update() on the items in the vector and the derived class update() functions be called. Currently, it always calls the Sprite update, which makes sense, but it's not what I want. Is there a way to call the derived update function without knowing the type of the derived class?

View 6 Replies View Related

C/C++ :: Template Class Function Taking A Derived And Generic Object

Feb 23, 2014

I'm trying to write a function for receiving messages, so my classes can communicate with each other. The only issue I get is a compile error asking me to define the base parameter as one of the derived instances. I tried using a void* to fill the need, but then I lose the initial type, which I need to check for. How might I go about writing a generic object for this?

Here's my code:

template<class Object>
class State
{
public:

[Code].....

Should I just have all of the objects inherit in the order of Object >> GenericObject >> DerivedObject?

View 10 Replies View Related

C++ :: Member Function In Derived Class Call Same Function From Its Base Class?

Sep 18, 2013

How can a member function in my derived class call the same function from its base class?

View 1 Replies View Related

C++ :: Pass 2 Arrays Into Void Function And Return Values To One Function?

Feb 12, 2014

I'm trying to pass 2 arrays into a void funtion, and return values to one function.

this is the the program I'm working with, after I'm done I have to split it into 3 files, a header, a main, and a separate cpp file for the functions to live in.

#include <iostream>
using namespace std;
void processArrary(int numberCount[], int Numbers[], int intnumberSize, int numberCountSize);
int main() {
int Scores[26] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189};
int numberCount[8] = { 0 };

[code]...

The goal of this program is to separate and count the groups of numbers then output the amount of numbers in each group. Near as I can tell, everthing should work, but I'm getting all zeros to be displayed in each group.

View 6 Replies View Related

C :: Main Function Does Not Return Any Values When Calling Other Function?

Jun 9, 2013

The function is supposed to return value from the file in my main, but I am getting empty value. I am trying to get better with pointer. Right now just teaching myself.

right now the only way for this code to show value is when in put the putchar(*ps) inside my readfile function. I would like to readfile to return value and print in the main function.

Code:

#include <stdio.h>
char *readfile(char filename[]);
int main(int argc, char *argv[] ) {

[Code].....

View 4 Replies View Related

C++ :: Calling Derived Class Functions In A Function With Parameter Of Base Class

Mar 30, 2013

Say I have 3 classes:

class Player {
public:
virtual func1();

[code]....

Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:

//function fight()
fight(Player p1, Player p2) {
p1.func2();
}
//using function fight()
fight(human, computer);

When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).

View 6 Replies View Related

C++ :: Derived Class Not Overwriting Base Class Function - Using Vectors

Feb 4, 2014

So I have a base class, lets call it base. In base I have a virtual function called update(), update just couts "base" then I have a class derived from base called derived;

it has a function called update(), update just couts "derived" then I create a vector called Vec it's initialised like this:

std::vector<base> Vec;

then I add an element into it like this

Derived DerElement;
Vec.push_back(DerElement);

then when I type:

for (int i=0; i<Vec.size(); i++) {
Vec.at(i).Update();
}

It outputs:

Derived DerElement2;
DerElement2.Update();

and it outputs this:

#include <iostream>
#include <vector>
class Base {
public:
virtual void Update() {

[Code] .....

and this is it's output:

Base
Derived
Press any key to continue . . .

View 1 Replies View Related

C++ :: Can Base Class Call Overridden Function From Derived Class?

Aug 28, 2013

I just wondering if a base class can call the overridden function from a Derived class?

Here's an example:

//Base Class H
class BaseClass {
public:
BaseClass();
virtual ~BaseClass();
virtual void functionA();

[Code] ....

So basically, when I am creating a new object of Derived class, it will initialize BaseClass and the BaseClass will call functionA but I want it to call the function overridden by Derived class.

I know that if I call newObj->functionA it will call the overridden function. Right now I want the base class to call the overridden function "this->functionA(); in BaseClass" during its initialization. Is it possible to do that?

View 9 Replies View Related

C++ :: Undefined Reference To A Function

Jan 19, 2013

The error is this:

#include <iostream>
using namespace std;
void add(int s);
void subtract(int d);
void multiply(int p);
void divide(int q);

[Code] .....

View 2 Replies View Related

C++ :: Undefined Reference To A Function?

Nov 23, 2013

I'm making a program that's essentially a Text-Based Fire Emblem game; it runs calculations and rolls dice and has all sorts of Goodies. However, I have hit a block to the tune of

#ifndef ITEM_H
#define ITEM_H
class Item
{

[Code]....

Up Until I called up a Sword object, it worked fine. But when I compiled it, I got an Undefined Reference to Item::Item() error in Line 8 of Weapon.cpp.

View 2 Replies View Related

C++ :: Returning By Reference From Function

Feb 15, 2015

#include<iostream>
using namespace std;
int &fun() {
int x = 10;
return x;
}
int main() {
fun() = 30;
cout << fun();
return 0;
}

The code outputs 10.

Shouldn't it show an error because x is created locally on stack and gets destroyed on function return?

View 1 Replies View Related

C++ :: Undefined Reference To (function Name)

Oct 2, 2014

Everything seems to be in order and I know my code still has mistakes. I'm just trying to get it to compile and it won't allow it. I've narrowed it down to when I call the functions in main but beyond that I have no clue.

#include <iostream>
#include <cstring>
using namespace std;
void getSize(int num);
void getSpace(int num, int ptr);
void inputData();
void printData();
void destroy();
const int BIG_NUMBER = 100;

[code]....

View 4 Replies View Related

C++ :: Undefined Reference To Function?

Oct 24, 2013

So I have a really strange problem occurring...

First, here are the files I'm using:

//pa4.cpp wirtten by Syd Frederick
#include<iostream>
#include<string>
#include<fstream>

[Code].....

When compiling I'm getting a strange error that says :

/tmp/ccdt0Bf9.o: In function `main':
pa4.cpp:(.text+0x1c): undefined reference to `synopsis()'
pa4.cpp:(.text+0x1e7): undefined reference to `execute(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status

View 2 Replies View Related

C++ :: Displaying Function From Reference?

Aug 29, 2013

Having a little trouble getting my function to display the results. I got my getNumber function working and I know I can't use return to return multiple values so I created a function that would display my results but I am a little lost on how I get the results from one function to another.

#include <iostream>
#include <array>
#include <iomanip>
using namespace std;
void printResult(int, int);
void getNumber(int &, int &);
void printResults (int, int){
int n, n1;

[code].....

View 13 Replies View Related

C++ :: How To Call Function From Derived Class In Base Class

Dec 24, 2013

Basically, I have a base class called MainShop and it has 3 derived classes which are SwordShop, SpellBookShop and BowShop. I want the base class to be able to call a function from one of the derived classes but no matter what i do, it doesn't seem to work!

Here is my code:

#include "MainShop.h"
//BaseClass cpp
void MainShop::EnterShop(Hero& hero)

[Code]....

I have two other derived classes, but its basically the same concept. I have a function in one of the derived classes and i would like to call it from the base class. This is one my derived classes:

//SwordShop derived cpp
#include "SwordShop.h"
void SwordShop::soldierShop(Hero& hero)
{
/* some code here*/
}

View 4 Replies View Related

C++ :: Using Pointers Instead Of Reference Variables In Function

Mar 26, 2013

The following function uses reference variables as parameters. Rewrite the function so it uses pointers instead of reference variables, and then demonstrate the function in a complete program.

int doSomething(int &x, int &y)
{
int temp =x;
x = y * 10;
y = temp * 10;
return x + y;
}

I understand how to covert the reference variables to pointers, however I am stuck on this error. Either I get the error listed in the title or (with a few changes) the error "invalid conversion from 'int' to 'int*'"

What am I doing incorrectly?

#include <iostream>
using namespace std;

int doSomething(int*, int*);

int main(){
int X, Y, result;

[Code] ....

I have multiplied both x and y by 10 and then added them together!

Here is the result " //I really didn't know how else to use the "doSomething" function in a meaningful way. So... I just stated what the function does.

<< result << ".
";
system("PAUSE");
return 0;
}
int doSomthing(int *x, int *y)

[Code] .....

View 1 Replies View Related

C++ :: Attempting To Reference A Deleted Function

Feb 20, 2015

Been away from c++ for a few months now (apart from answering a few questions on this site). I was trying to see if I could do a simple program using just my memory that involved unique_ptr's, but getting an error:

"attempting to reference a deleted function". I thought that by getting a reference to my pointer would not invoke a copy constructor but i'm clearly wrong.

Here's the code:

#include <iostream>
#include <memory>
#include <vector>
class Shape {
public:
virtual void Draw() = 0;

[Code] ....

View 5 Replies View Related

C++ :: Reference To A String In Function Parameters

Feb 22, 2015

So I was reading my book and it listed this piece of code. The first piece of code is in the book and the 2nd is just my test on the piece of code. I am curious as to why in the functions parameters there is a reference to aString. I've noticed that removing it has no affect on the outcome of the code.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool isPalindrome (string & aString) {

[Code] ....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved