C++ :: Error Returning By Reference From Function

May 14, 2012

struct emp {
char name[20];
int age;
float sal;
}e1 = {"hello", 24, 2345.67}, e2 = {"hi", 34, 45678.89};

[Code] ....

Query: I am unable to get emp &fun() and fun() = e2...

View 3 Replies


ADVERTISEMENT

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++ :: Error Returning Object From Function?

Nov 24, 2013

Code:

class Main{
class B b_obj;
C c_obj=b_obj.test("some",89); // I cannot get this done.!
}

Below are .h files:

class B{
public:
C* test(const char *, int); /// this doesn't work
constructor
//member variables

[Code] ....

However, map<string, vector<bol>> test(const char *, int); works.I know second thing will work, but how to take reference of class object?

I have include .h file in main, B and C, wherever it is required.

View 4 Replies View Related

C++ :: Error Returning Template Class From Member Function

Apr 30, 2012

When I do this:

// header file:
#include <list>
using namespace std;
class myClass {
list<int> myMethod();
};

// cpp file:

list<int> myClass::myMethod() {
}

In the cpp file, 'myMethod' is underlined in red and when I hover over it, it says:

"std::list<int, std::allocator<int>> myClass::myMethod()

Error: declaration is incompatible with "<error-type> myClass::myMethod()""

But when I make it as a standalone function, outside a class, with no pre-declaration, there is no problem.

View 8 Replies View Related

C++ :: Undefined Reference To Function Error

Nov 19, 2013

I am currently working on a code, but I keep getting a error as follows:

[Linker error] undefined reference to 'getHoursOfTheWeek(double)'
[Linker error] undefined reference to 'getGrossPay(double)'
[Linker error] undefined reference to 'getStateTax(double)'
[Linker error] undefined reference to 'getFederalTax(double)'
[Linker error] undefined reference to 'getFICA(double)'
[Linker error] undefined reference to 'getWithHoldingAmount(double)'

I have been trying to debug this error for hours and I am completely stumped....

//BEGIN
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
string getUserName();
double getPayRate();

[Code] .....

View 2 Replies View Related

C/C++ :: Getting Undefined Reference Error To A Function

Mar 16, 2015

I'm getting an undefined reference error to a function, and I can't figure out why. I have tried letting code blocks compile the files, I have tried the command line to compile it with the same results.

I looked up the error and found this from [URL]

undefined reference
Example
/tmp/cc2Q0kRa.o: In function `main':
/tmp/cc2Q0kRa.o(.text+0x18): undefined reference to `Print(int)'
collect2: ld returned 1 exit status

Meaning / Your code called the function Print, but the linker could not find the code for it in any .o file

Usual Causes

You forgot to link the .o file that contains the function

You misspelled the name of the function

You spelled the name of the function correctly, but the parameter list is different in someway

which seems to be the error I get. I have double checked all 4 and I see nothing.

The code that specifically gives me the error is:

Item *name = new Item(desc, id, weight, loc);
itemMap.addItem(name);

and the class looks like this:

class Item // Standard Items {
private:
std::string name;
std::string desc;
int id;
int weight;
int loc;

[code].....

I think everything matches up unless I'm just missing it.

Full codes are located here: [URL]

what I'm doing wrong?

View 5 Replies View Related

C++ :: Error - Reference To Non Static Function Must Be Called?

Feb 27, 2013

So on lines 36 - 39 (The commented out functions) is where I'm sure is causing this error because once I don't comment them out pretty much everywhere Flink or Rlink is used or defined I get this error.

#ifndef nodes_Nodes_h
#define nodes_Nodes_h
#include <cstdlib>

[Code]....

View 4 Replies View Related

C++ :: In Function Main Undefined Reference Error

Apr 19, 2012

Why I keep getting the error: "In function main: [linker error] undefined reference to readBook(BookData)" ....

main.cpp :

Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "BookData.h"
// FUNCTION PROTOTYPES:
void bookInfo(BookData);
void readBook(BookData& book1);

[Code] ....

View 9 Replies View Related

C++ :: Returning Reference Of Vector

Sep 6, 2013

Example code:

Code:
#include <iostream>
#include <vector>
using namespace std;

class A{

[Code]....

I read somewhere, that we can imagine the reference as a pointer to the vector. So, my question is:

Let's assume that instance of class A, named a, was created with new. We call a.getV() to foo and then we call the destructor of a. foo is safe? Is the copy constructor of std::vector called?

View 1 Replies View Related

C++ :: Returning Object By Reference

Jan 25, 2014

When returning an object by reference, only the address of the returned-object is returned, and that way we spare pushing a large object into the stack, and also spare time of pushing and popping large object to/from stack.

But what happens when the object that receiving the returned-object, is not a reference, but a 'regular' object?

How is the content of the returned object copied into the receiving object?

See for example in main, wid vs rwid. (I know in the case the returned-object is just one variable, there's no need to return it by reference, but its for simplifying the code).

class Rectangle {
public:
Rectangle(int w=0, int h=0);

[Code].....

View 6 Replies View Related

C/C++ :: Returning Reference To Object

Aug 1, 2014

I have this method that takes a pointer to a class object and right now when printing it, it's returning the location in memory 0x100300000. I've tried tweaking the function in a few different ways but I cant get it to return the object instead of the location.

Here's the vector that addSale accesses and the deceleration of the addSale ethod in the employee class.

class Employee{
protected:
....
public:

[Code]....

View 4 Replies View Related

C++ :: Undefined Reference Error When Accessing Static Variable Inside Member Function

Feb 10, 2013

I am modifying a set of static variables inside of the class's member function. The static variables are private. An example of what I'm doing is as below,

utilities.h
-----------
class utilities {
private:
static int num_nodes;

public:
void parse_details(char* );

[Code] ....

I get a compilation error in the function void utilities::parse_details(char* filename)

which says: undefined reference to `utilities::num_nodes'

compiler: g++

View 2 Replies View Related

C++ :: Call By Reference And Returning Wrong Value

Dec 17, 2013

Write your question here.

#include<iostream>
using namespace std;
void getInput(int& numOfDays, double& itemPrice);

[Code].....

So this program is supposed to calculate the price of an item, and return it back as the variable total of type int. but for some reason it gives me a weird output on total. And am i using call by reference correct?

View 1 Replies View Related

C++ :: Error Passing Object Reference

Nov 23, 2013

Code:

#include <Data.h>
int main(int arg_count, char** argv) {
if(arg_count!=3){
cerr<<"Files misisng
";
exit(1);

[code]...

This actually should work, because it is passing address of polymorphisms object.I have tried changing prototype of test in Data.h, but failed.passing object address/pointers in C++.

View 5 Replies View Related

C++ :: Error - Undefined Reference To PrintSpecChar (int)

Apr 2, 2013

I just purchased a laptop with Windows 8 and installed the portable Dev-C++ because the other one wasnt compatible with Win8.

Now im getting an error when i compile that says:

'undefined reference to 'printSpecChar(int)

when i was using Dev-C++ on Win7, i never had to use #include <stdlib.h> for system ("pause") but now I do.

Is there a setting that I need to change, or am I just missing something.....again?

Code:
#include <stdlib.h>
#include <iostream>
using namespace std;
int getNumber(int, int);
void printSpecChar(int);
void pause (double);
int main() {
int n;

[code]....

View 6 Replies View Related

C++ :: Error - Invalid Initialization Of Non-const Reference Of Type

Feb 11, 2013

I am trying to use the Singleton design patterno on Linux, but it is not working:

in the .h I have:

Code:
public:
static ErrorH& Instance();
private:
ErrorH() {};

In the .cpp I have:

Code:
ErrorH& ErrorH::Instance() {
static ErrorH& self = ErrorH();
return self;
}

The errors I get are:

Code:
g++ --g -c ErrorH.cpp -o ErrorH.o
ErrorH.cpp: In static member function "static ErrorH& ErrorH::Instance()":
ErrorH.cpp:9: error: invalid initialization of non-const reference of type "ErrorH&" from a temporary of type "ErrorH"
make: *** [ErrorH.o] Error 1

This code works on Windows, how can I get it to work on Linux?

View 2 Replies View Related

C++ :: Function Returning A Value

Feb 20, 2015

I am making a game commonly know as the Hangman using C++.

Now I am trying to add a man in it like this:

0
|/
|
/

Now the problem i am facing is that i am using a check that if a function returns the value 0 "return 0" it means the guess is wrong and it will not update the man but if it returns any value there will be a function called which will update the man.

I just wanna know that how i am going to use the check, the kind of thing that i am trying to use is, in general words "if(function returns a value) then update the man"

int main() {
return match;
}

How are we going to use it in check that if int main is returning 'match' in the check...

View 3 Replies View Related

C++ :: Int Function Not Returning A Value

Jan 22, 2014

This simple little program is not returning a value. The output is

" (string) contains characters" (The number of characters is supposed to display between the 'contains' and 'characters.'

However, if I go to the function and cout the length, the cout in the main body displays just fine.

Here's the main portion :

cout << "'" << input << "' contains "; //Output of character count.
charCount(input);
cout << " characters, including any spaces.
"; //Output of character count.

and here's the function.

int charCount(char *string) {
int length = 0; //Variable to hold the number of characters.
//Gets the number of characters contained in *string and puts that number into length.

[Code] .....

View 2 Replies View Related

C :: Returning Pointer From Function

Nov 21, 2014

As the title says, i'm using a function which returns a pointer to a struct:

the struct is the following:

Code:
typedef struct POINT
{
uint16_t x;
uint16_t y;
}

Coordinate; the function i'm using:

Code:
Coordinate * Read_XTP2046(void)
{static Coordinate screen;
//calculations to determine the coordinates
screen.x=(temp[1]+temp[2])/2;
screen.y=(temp[0]+temp[2])/2;
// and so on...
return &screen;}

The question is: how do i catch this pointer and make it into a Coordinate struct in which i can read the x and y.

In my main program i would do the following:

Code:
Coordinate cor;
cor = Read_XTP2046();

This does not work, as the function returns a pointer, but how to transform this pointer into a Coordinate struct.

View 8 Replies View Related

C :: Returning Back To Another Function

Mar 7, 2014

Here's a small portion of my program:

Code:
int choice(void) {
char buffer[BUFSIZ];
char answer;
printf("

[Code] .....

I am wondering which is correct to use

Code: return choice();
or
Code: choice();
return num;

View 4 Replies View Related

C :: Returning Function To Array?

Mar 31, 2013

I need to create a function which will print a list from 100Hz to 1000Hz then 1000Hz to 9000Hz. I have created a function in order to calculate and set up the frequency values from 100Hz to 9000Hz using two for loops as shown below. However I am unsure how to return this to the array at the main.

int main(void) {
double Frequency[18];
system ("PAUSE");
return(0); } double Frequency (void)
{
int count;

[Code]....

View 1 Replies View Related

C :: Returning Value From One Threaded Function To Another

Apr 15, 2013

I am using two threads and i want to take value of a function from one thread and use it in other. I am not good at the concepts of threads. Here is the following code:

Code:
void ThreadA(void const *argument){
uint32_t status = I2S002_FAIL;

status = I2S002_Config(&I2S002_Handle0, &I2SConfig_U0C1_A);
if (status != DAVEApp_SUCCESS) {

[Code] ....

So, i want to use the return value of temp_buffer from ThreadB into Thread C and want to put this value to TXBuf in ThreadA...

View 1 Replies View Related

C++ :: Why Function Is Not Returning Integer 1 Or 0

Nov 14, 2013

why the function is not returning the integer 1 or 0 ... We have two arrays A and B, each of 10 integers. Write a function that tests if every element of array A is equal to its corresponding element in array B. The function is to return true (1) if all elements are equal and false (0) if at least one element is not equal.*/

#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;
int TEST (int arrayA[], int arrayB[], int j);
int main() {
srand (time(NULL));

[code].....

View 2 Replies View Related

C++ :: Expression - Value Returning Function

Dec 17, 2014

How is the definition of the term "expression" affected by value returning functions, and why?

View 5 Replies View Related

C++ :: Returning Dynamic Value From A Function

Nov 10, 2014

Is this considered a memory leak?

Polygon* create_square(const Vector2d& position, const RGB& colour, const std::string name) {
// Polygon, Vector2D, RPG and Vertex2d are structs with public data members only
Polygon *temp = new Polygon;
temp->name = name;
temp->colour = colour;
temp->position = position;
temp->vertices = new Vertex2d;
return temp;
}

View 1 Replies View Related

C++ :: Returning Array From A Function?

Jun 17, 2013

I am trying to neaten my code by putting them in different cpp files but in one function an array is changed:

int function(int array2[100][9])
{
for (int i =0; i < 100; i++)

[Code]....

View 9 Replies View Related







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