Advertisement

C++ Workshop - Arrays & Strings (Ch. 13)

Started by September 04, 2006 03:08 PM
35 comments, last by alphastickmania 15 years, 8 months ago
You need to explicitly state you expect an array by naming the parameter 'string theArray[]', or you can use a pointer such as 'string* theArray'.
where, in the call? in the called function?

#include <iostream>#include <string>using namespace std;void passTheArray(int, string);int main (){  string * theChoices = new string[3];  theChoices[] = "bla", "blo", "blu"};  passTheArray(5, theChoices);  cin.get();  return 0;}void passTheArray(int theInt, string theArray[]){  cout << endl << theArray[0];  cout << endl << theArray[1];  cout << endl << theArray[2];}


"conversion from `std::string*' to non-scalar type `std::string' requested"
Advertisement
#include <iostream>#include <string>using namespace std;void passTheArray(int, string);int main (){  string * theChoices = new string[3];  theChoices[0] = "bla";//, "blo", "blu"};  theChoices[1] = "lal";//, "blo", "blu};  theChoices[2] = "heh";//, "blo", "blu};  cout << endl << theChoices[0];  cout << endl << theChoices[1];  cout << endl << theChoices[2];  passTheArray(5, *theChoices);  cin.get();  return 0;}void passTheArray(int theInt, string theArray[]){  cout << endl << theArray[0];  cout << endl << theArray[1];  cout << endl << theArray[2];  }

[Linker error] undefined reference to `passTheArray(int, std::string)'
ld returned 1 exit status
[Build Error] [arrayPassing.exe] Error 1

[Edited by - kingIZZZY on October 10, 2006 6:08:05 PM]
kingIZZZY:

Take a look at the function declaration at the top of your file. It's declaring the function to take an int and an std::string. The function that you write down at the bottom takes int and std::string[]. Notice that an array of std::strings is not the same thing as a single std::string. Change the pre-declaration to be the same as the function.

Hope that helps.
sure did,
void passTheArray(int, string[]);

thanx.
Hey guys. Very helpful tutorial here. I only found it a couple of weeks ago and have been playing catch-up since. I have some previous C++ experience, mainly coding academic numerical simulations - nothing really using the full scope of object oriented design. I've found it fairly quick going up until the last couple of days. I've run aground though.

I'm trying to implement a [] operator on a singly-linked list. The list has a head-node object and a linked set of body-node objects. Each body-node includes an object of the data item class (i.e. the things that this is a list of.) There is a 'supervisor' object that organises adding objects, iterating over 'printout' type functions, and this is delegated to from objects of another class (i.e. the objects that 'carry' the list of 'items' around - this may be an inventory management system ;) )

I've overloaded (correct term?) the index [] operator for the list manager class to return a pointer the data item contained by the n'th body node. This is the first [] operator I've ever overloaded, though I'm accustomed to overloading other operators and I'm pretty happy that this is implemented correctly, and I have even checked against a listing in the book and my code is almost identical. I wasn't copying that listing, but the method is structurally very similar. I must be missing something!

Basically, in the list management class:
invItem * operator[](int) const;

The problem arises when I try to then use the operator to get the pointer to the item. If I use itemList to get the i'th item, I'm expecting it to return a pointer to an item invItem as requested in the above (and in the definition.) When I try to compile, for example:
invItem * myItem = invManager[2];
I get a compile-time error saying that invManager[2] is returning a pointer to its own class, not a pointer to an invItem.

I've even hovered my mouse over each item in the list (VC++05) and can see that the context-sensitive pop-up think that it's supposed to be returning a pointer to invItem, but then yacks when I try to compile.

Any clues? Overloading the [] operator in this context is mentioned in Ch13 of the book, but not explained, and then it's not clear how to use it from the context when it pops up again in Ch15.

Cheers
L
-

Visit http://www.mugsgames.com

Stroids, a retro style mini-game for Windows PC. http://barryskellern.itch.io/stroids

Mugs Games on Twitter: [twitter]MugsGames[/twitter] and Facebook: www.facebook.com/mugsgames

Me on Twitter [twitter]BarrySkellern[/twitter]

Advertisement
I've noticed no one has answered your question yet. I'm not sure of the answer myself, but if you perhaps posted your code in [ source] boxes? Just write [ source] for the opening (no space between [ and s) and [ /source] for the close(no space between [ and /).

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

What type is 'invManager'?
If it is a pointer to your manager (if the head node is of a special type) or node class, then "invManager[x]" is returning "invManager + x" (pointer arithmetic) instead of "invManager->operator[](x)". To solve this, use the previously-stated code, "(*invManager)[x]" to dereference it, or a get method.
Otherwise, I don't know what to tell you.

Also, depending on how you're going to be using the list and how it's structured, you could have the operator return a reference:
invItem& Manager::operator[](int x) const;
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Thanks for the replies, but I already worked around this by what now seems like a ridiculouly simple method: I read more! So I'm now using std::lists and std::vectors to do much of what I was previously doing manually. This has raised questions in itself, mainly with iterators not doing quite what I expect, but no showstoppers and it's far more productive than writing every linked list myself.

L
-

Visit http://www.mugsgames.com

Stroids, a retro style mini-game for Windows PC. http://barryskellern.itch.io/stroids

Mugs Games on Twitter: [twitter]MugsGames[/twitter] and Facebook: www.facebook.com/mugsgames

Me on Twitter [twitter]BarrySkellern[/twitter]

Hello ladies and gents,

Seeing as no answer thread was created, ... again, I'll post my answers to the great Quiz Questions from JWalsh here.


Really don't understand why so little amount of people took advantage of this?

Chapter 13.

1) What is an array?
A) An array is a sequential collection of data storage locations, each of which holds the same data type.

2) How do you access an element in an array?
A) You access an array element by referring to its offset from the beginning of the array.

3) Are offsets (indexes) numbered starting from 0 or from 1?
A) They are started from 0.

4) What happens if you write to an index that is outside the range of your array?
A) If you're lucky, your program will crash, if not, then you'll get strange results much later in your program, and you'll have a difficult time figuring out what went wrong.

5) What are fence post errors?
A) It's writing one past the end of the array, it is such a common error(bug), that it got it's own nick name.

6) How do you initialize arrays with simple data types?
A) After the array name, you put an equal sign (=) and a list of comma-separated values enclosed in braces. For example:
int myArray[5] = { 0, 1, 2, 3, 4};

7) If you initialize an arry with values, are you required to specify the size? Should you?
A) No, you're not required to do so, but you should.

8) When using an array of objects, which constructor gets called when the array is created?
A) The default constructor is called.

9) Is it possible to have multi-dimensional arrays?
A) Yes, that is possible, each dimension is then represented as a subscript in the array

10) Is there a maximum number of dimensions which are supported by C++?
A) You can have as many dimensions as you want, although it is likely that most of the arrays you create will be of one or two dimensions.

11) In C++, what is another way of thinking about an array name, as it pertains pointers?
A) An array name is a constant pointer to the first element of the array.

12) Is is legal to use array names as replacements for the construct in question? How about vice-versa?
A) Yes, it is legal to use array names as constant pointers and vice-versa.

13) What happens if you allocate a dynamic array of memory and don't delete it, including the [] operator?
A) You create a memory leak.

14) Can you set the size of an array created on the stack at run-time?
A) No, you can't, the size has to be determined at compile-time.

15) Can you set the size of an array created on the heap at run-time?
A) Yes, that is the biggest advantage of creating an array on the heap.

This topic is closed to new replies.

Advertisement