The McLeodGaming forums were permanently closed on April 30th, 2020. You are currently viewing a read-only archive.
View unanswered posts | View active topics It is currently Fri May 15, 2020 9:29 am



 [ 26 posts ]  Go to page 1, 2  Next
More C++ help 
Author Message

Joined: Wed Oct 21, 2009 4:23 pm
Posts: 107
Gender: Male
To continue Captain Mew's inactive topic.

If anyone needs help, please, feel free to ask.


Sat Oct 24, 2009 2:41 pm
User avatar

Joined: Tue Nov 17, 2009 1:26 am
Posts: 648
Gender: Anime Girl
I have a question regarding the use of arrays in functions.

I have several errors in my program that have to do with arrays in my function heads:

Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

...

void Function (char & A[ ]) {
...
}


My compiler (Dev-C++) however gives me the following error:
Code:
line 307 ... declaration of `A' as array of references


It would be vital to make the arrays call by reference in many cases. Does anybody know how to solve this error?

_________________
Image
Liberal Socialist Mudraking Bastard (Averted, not performing any journalism)


Wed Nov 18, 2009 9:30 am
User avatar

Joined: Sat Aug 16, 2008 8:38 am
Posts: 6670
Location: Darkest Antartica
Country: Pakistan (pk)
Gender: Male
Skype: Thaiberium
Currently Playing: The Game
What's with the & in your function?

That and have you read documentation concerning passing an array of undefined sized as an argument to a function? I'm not sure if it can be done or not.

Consider using pointers.

_________________
Image


Wed Nov 18, 2009 11:39 am
User avatar

Joined: Tue Nov 17, 2009 1:26 am
Posts: 648
Gender: Anime Girl
I have seen sheets using arrays of undefined size in the function header. My book (Absolute C++) also shows arrays with no defined size.

I used the & because it needs to be call by reference, the function is supposed to alter the values of the array. I am fairly sure that is where the source of the problem lies, but I do not know how to make it call by reference otherwise (the book does not address call by reference arrays).

_________________
Image
Liberal Socialist Mudraking Bastard (Averted, not performing any journalism)


Wed Nov 18, 2009 3:32 pm
User avatar

Joined: Sat Aug 16, 2008 8:38 am
Posts: 6670
Location: Darkest Antartica
Country: Pakistan (pk)
Gender: Male
Skype: Thaiberium
Currently Playing: The Game
Wait. Are you using pointers by any chance?

_________________
Image


Thu Nov 19, 2009 12:30 am
User avatar

Joined: Tue Nov 17, 2009 1:26 am
Posts: 648
Gender: Anime Girl
I am not in this part of the program, but I don't think I am supposed to use them there.

I've just found that it is impossible to use an array of references, though.

EDIT: Okay, I think sorting the arrays out will be fine. However, I have another question:

I would like to use a member function in a non-member function, but I can't get it to work.

_________________
Image
Liberal Socialist Mudraking Bastard (Averted, not performing any journalism)


Thu Nov 19, 2009 2:10 am
User avatar

Joined: Sat Aug 16, 2008 8:38 am
Posts: 6670
Location: Darkest Antartica
Country: Pakistan (pk)
Gender: Male
Skype: Thaiberium
Currently Playing: The Game
I'm no longer any good on C++, can you just show me the code?

_________________
Image


Thu Nov 19, 2009 12:11 pm
User avatar

Joined: Tue Nov 17, 2009 1:26 am
Posts: 648
Gender: Anime Girl
I got it to work, here is how it works:

Code:
class monkey {
   ...
   public:
      ...
      funkymunky (int Eek, char Ook);
      monkey ( );
} // monkey end of class

monkey::monkey ( ) {
   ...
} // monkey end of default function

...

int main ( ) {
   ...
   monkey Ack;
   ...
   monkey.funkymunky (x , y);
   ...
   return 0;
} // int main

_________________
Image
Liberal Socialist Mudraking Bastard (Averted, not performing any journalism)


Last edited by Villerar on Sat Nov 21, 2009 1:32 pm, edited 1 time in total.



Thu Nov 19, 2009 11:48 pm
User avatar

Joined: Sat Aug 16, 2008 8:38 am
Posts: 6670
Location: Darkest Antartica
Country: Pakistan (pk)
Gender: Male
Skype: Thaiberium
Currently Playing: The Game
I never tried object oriented programming in C++, so I can only guess that the function monkey returns a class monkey. Or would that be a constructor using C++?

_________________
Image


Fri Nov 20, 2009 11:19 am
User avatar

Joined: Tue Nov 17, 2009 1:26 am
Posts: 648
Gender: Anime Girl
The function monkey provides a default frame for non-member functions to call member functions of a specific class.

To be honest, I do not know the difference amongst class, struc and constructor.

_________________
Image
Liberal Socialist Mudraking Bastard (Averted, not performing any journalism)


Sat Nov 21, 2009 1:35 pm
User avatar

Joined: Sat Aug 16, 2008 8:38 am
Posts: 6670
Location: Darkest Antartica
Country: Pakistan (pk)
Gender: Male
Skype: Thaiberium
Currently Playing: The Game
Code:
class Foo {
   private String a;
   private int b;
   //Constructor to create an object of class Foo
   public Foo (String a, int b){
      this.a=a;
      this.b=b;
   }
   //Some methods or class functions   
   public void Bar(int x){
      b += b + x;
      System.out.println(a " + " b);
   }
   
   public int getB(){
      return b;
   }
}

Above is a simple class from Java. What I mean by constructor is the function that creates an object of the class it belongs to.

_________________
Image


Sun Nov 22, 2009 3:05 am
User avatar

Joined: Tue Nov 17, 2009 1:26 am
Posts: 648
Gender: Anime Girl
In that case, monkey would be a constructor of the class monkey, yes.

_________________
Image
Liberal Socialist Mudraking Bastard (Averted, not performing any journalism)


Sun Nov 22, 2009 9:32 am

Joined: Wed Oct 21, 2009 4:23 pm
Posts: 107
Gender: Male
Villerar wrote:
The function monkey provides a default frame for non-member functions to call member functions of a specific class.

To be honest, I do not know the difference amongst class, struc and constructor.

Constructors are used when you initiate a class.

ex.
Code:
#include <iostream>
using namespace std;
class Test{
public:
 Test(); // Void initialized
 Test(int a, unsigned long b); // Another void that can be initialized
int at;
unsigned long anothertest;
};

 Test::Test()
{
   cout << "yeah" << endl;
}

 Test::Test(int a, unsigned long b)
{
    at = a;
    anothertest=b;
}


// now when you call this s***, you can call it in 2 ways

int main() {
Test test(4, 5);
Test la;
cout << "int test equals " << test.at << endl;
cout << "unsigned LONG equals " << test.anothertest << endl;
system("pause");
return 0;

}


Build, Compiled successfully with VS2008

examine yourself

EDIT: Never seen the question get answered, but this is more of a C, C++ version


Fri Nov 27, 2009 6:42 pm
User avatar

Joined: Sat Aug 16, 2008 8:38 am
Posts: 6670
Location: Darkest Antartica
Country: Pakistan (pk)
Gender: Male
Skype: Thaiberium
Currently Playing: The Game
Masked Man wrote:
Never seen the question get answered, but this is more of a C, C++ version

Sorry, I main on Java.

_________________
Image


Sat Nov 28, 2009 3:21 am

Joined: Wed Oct 21, 2009 4:23 pm
Posts: 107
Gender: Male
Thaiberium wrote:
Masked Man wrote:
Never seen the question get answered, but this is more of a C, C++ version

Sorry, I main on Java.

eh, Java is a good language, especially if you want to start making games. But it feels more, limited to me. It looks too much like C#, and I just don't understand it.


When I was learning Java, I remember for a window, you use JFrame. But for menu's, you have to create, like, 3 different classes :/ I just couldn't understand it.


Sat Nov 28, 2009 8:10 am
Display posts from previous:  Sort by  
 [ 26 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software for PTF.