C++ - Pass-by-reference

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 5411 Views - Last Post: 18 July 2010 - 09:16 PM Rate Topic: -----

#1 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

C++ - Pass-by-reference

Posted 17 July 2010 - 02:55 PM

After looking at tons of videos and websites i now understand
pointers and pass-by-ref & value.

But im still learning c++ and as a result of that i can't
figure how c++ does it.

Here is my attempt at pass-by-reference:
Main class
// operating with variables

#include <iostream>
#include "Character.cpp"

using namespace std;

int main ()
{
  // declare variables
  int result;
  int a;
  
  a = 1;
  
  // stuff
  Character ch;
  result = ch.Calculate_values(a);
  cout << result;
  
  // stall the program
  cin.get();
  
  return 0;
}



Character class
#include <iostream>
using namespace std;

class Character {

  public:
  int Calculate_values (int&, int&);
  
};

int Character::Calculate_values (int &a) {
     return (a = 5);
}



I get a compiler error that won't go away:
- multiple definition of `Character::Calculate_values(int&, int&)'

Is This A Good Question/Topic? 0
  • +

Replies To: C++ - Pass-by-reference

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 03:01 PM

You have the declaration that takes two int refs and the implementation takes one. they need to match.

Also, you can't return 'a = 5', at least in this context, it's not what you want. Assign the value to the reference parameter to achieve what i think you're looking to do.

You don't even need to assign the return value to another variable (or have a return value at all, that completely defeats the purpose of passing a reference).


someFunction(int&, int&);
someFunction(int&); //not equivalent with the above


void refFunction(int& a){
     a = 5;
     return; //'a' in main will now be five
}

int refFunction(int& a){
     a = 5;
     return a; //if you wasted to do this 
}


Was This Post Helpful? 2
  • +
  • -

#3 c0dy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 34
  • Joined: 29-June 10

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 03:07 PM

In line 7 and line 11 of your character class... they must have
the same signature. Your declaration takes two ints for parameters
and your definition of that function takes only 1 int.

But I am not sure what you're trying to do. Calculate_values isn't
calculating anything. It's assigning 5 to 'a' which, since it's
passed by reference, is going to replace whatever value that went
into the function from main.
Was This Post Helpful? 0
  • +
  • -

#4 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 03:22 PM

View PostKYA, on 17 July 2010 - 02:01 PM, said:

You have the declaration that takes two int refs and the implementation takes one. they need to match.

That was my bad i forgot to change the arguments back from two to one

Not sure why i had it return a value but anyway, using
character class
#include <iostream>
using namespace std;

class Character {

  public:
  void Calculate_values (int&);
  
};

void Character::Calculate_values (int& a) {
     a = 5;
}




Doesn't work either eith a compiler error ->
- multiple definition of `Character::Calculate_values(int&)'
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 03:37 PM

Any other files including this header? Whole code please.
Was This Post Helpful? 0
  • +
  • -

#6 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 04:11 PM

There is the above 'character'.cpp and the 'main'.cpp file ->
Main class
// operating with variables

#include <iostream>
#include "Character.cpp"

using namespace std;

int main ()
{
  // declare variables
  int a;
  
  a = 1;
  
  // stuff
  Character ch;
  ch.Calculate_values(a);
  cout << a;
  
  // stall the program
  cin.get();
  
  return 0;
}



This post has been edited by Java Student: 17 July 2010 - 04:14 PM

Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 04:14 PM

You have function definitions in a header file. That's contributing to the multiple definition error.
Was This Post Helpful? 0
  • +
  • -

#8 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 04:17 PM

View PostOler1s, on 17 July 2010 - 03:14 PM, said:

You have function definitions in a header file. That's contributing to the multiple definition error.

just .cpp files

Attached image(s)

  • Attached Image

This post has been edited by Java Student: 17 July 2010 - 04:18 PM

Was This Post Helpful? 1
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 04:19 PM

Oh, I'm sorry, you made the horrible, horrible mistake of including C++ files. You never include source files.

Please read up on how C++ works. Also worth reading the gamedev article.
Was This Post Helpful? 0
  • +
  • -

#10 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 04:32 PM

View PostOler1s, on 17 July 2010 - 03:19 PM, said:

Oh, I'm sorry, you made the horrible, horrible mistake of including C++ files ...[/url].


What other files am i supposed to include ?
Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 04:34 PM

You only include header files, if you have and need any.

This post has been edited by Oler1s: 17 July 2010 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

#12 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 04:43 PM

It's also a common practice to use include guards in your headers, which will prevent multiple inclusions.

One way is to put this code in your header:
#ifndef HEADERFILENAME_H
#define HEADERFILENAME_H

// Your code here

#endif 

or you could simply put this in the beginning of your header file:
#pragma once

This post has been edited by JITHU: 17 July 2010 - 04:43 PM

Was This Post Helpful? 1
  • +
  • -

#13 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 04:52 PM

View PostJITHU, on 17 July 2010 - 03:43 PM, said:

It's also a common practice to use include guards in your headers, which will prevent multiple inclusions.

Oh i never knew that, i,ll look up on that =)

I think i,ll try to figure this problem out on my
own, thanks all

This post has been edited by Java Student: 17 July 2010 - 07:11 PM

Was This Post Helpful? 0
  • +
  • -

#14 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 07:11 PM

So header files act as a kind of "bridge" between source files?
Was This Post Helpful? 0
  • +
  • -

#15 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • View blog
  • Posts: 378
  • Joined: 01-December 08

Re: C++ - Pass-by-reference

Posted 17 July 2010 - 07:38 PM

View PostJava Student, on 17 July 2010 - 06:11 PM, said:

So header files act as a kind of "bridge" between source files?

Header files define content, such as classes, which you can then use in your main file after including the header file. Also, when defining a class in a header file, a common practice is to declare the methods and then define them in a .cpp file with the same name. However, if you do that, you still only need to include the header file.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2