Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   viewing contents of array (http://www.programmingforums.org/showthread.php?t=11063)

RemoteC2 Aug 13th, 2006 9:53 PM

viewing contents of array
 
the object of the class that i made was to get the contents of a target array..this can only handle arrays with numbers so far, but that is not my problem..my problem is that i get the following errors after i compile this program:
G:\Secondary\Dev-Cpp\SOURCES\Chapter 14\vArray.cpp In member function `void vArray::getArray(char*, int)':
13 G:\Secondary\Dev-Cpp\SOURCES\Chapter 14\vArray.cpp `setx' is not a type
13 G:\Secondary\Dev-Cpp\SOURCES\Chapter 14\vArray.cpp request for member of non-aggregate type before '(' token
14 G:\Secondary\Dev-Cpp\SOURCES\Chapter 14\vArray.cpp `getx' is not a type
14 G:\Secondary\Dev-Cpp\SOURCES\Chapter 14\vArray.cpp request for member of non-aggregate type before '(' token

i really dont know what any of these mean so can someone please help me?

this is the code:
:

#include <iostream>
using namespace std;

class vArray
{
      int x;
      int i;
public:
      int getx() {return x;}
      void setx (int i) {x=i;}
      void getArray(char *arrayName, int arraySZ) {
                    for(i=0; i<arraySZ; i++) {
                              arrayName[i].setx(arrayName[i]);
                              cout<<arrayName[i].getx();
                    }
      }

}vA;

int main ()
{
    return 0;
}


King Aug 13th, 2006 10:14 PM

getx() and setx() are methods that are apart of the class vArray. You are trying to go:
:

arrayName[i].setx(arrayName[i]);
setx() is not part of a char*, it's a part of your class, you can't just tac that function on the back of anything you want.

Game_Ender Aug 13th, 2006 10:16 PM

arrayName[i] will return a char. char is a built in type, it does not have functions called setx, and getx. It looks like you have yourself a bit confused here. What is it you are trying to do.

Edit: Beat to it by King.

RemoteC2 Aug 13th, 2006 10:23 PM

i am trying to make a class that has a main function (getArray), and in getArray, it will return the contents of the target array (getArray(char *arrayName, int arraySZ)). i am a little bit confused as to what to do, but that is why i am here :) i hope that answered your question

RemoteC2 Aug 13th, 2006 10:33 PM

okay, now i see what i was doing wrong =)

King Aug 13th, 2006 10:33 PM

You don't really need a whole class just for this, a function would do just fine, take a look at this:
:

#include <iostream>
using namespace std;

void PrintArray(char* myArray[100])
{
        for(int i = 0; i != sizeof(myArray); ++i)
        {
                cout << myArray[i] << endl;
        }
}

int main ()
{
        char* myArray[100] = {"t", "e", "s", "t"};
        PrintArray(myArray);
        return 0;
}

If you do want a class, try this:
:

#include <iostream>
using namespace std;

class Array
{
public:
        Array()
        {
        }
        void PrintArray(char* myArray[100])
        {
                for(int i = 0; i != sizeof(myArray); ++i)
                {
                        cout << myArray[i] << endl;
                }
        }
};

int main ()
{
        char* myArray[100] = {"t", "e", "s", "t"};
        Array classArray;
        classArray.PrintArray(myArray);
    return 0;
}

Hope this clears things up a bit for you.

RemoteC2 Aug 13th, 2006 10:46 PM

wow..much easier :) i thank everyone for the help, this has really aided me


All times are GMT -5. The time now is 12:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC