ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm working my way through a book on C++ programming and am stuck on an exercise on passing a string to a function to have underscores replaced by hyphens, but I can't even pass a string to the function.
I can't get past this exercise, can someone please tell me what is wrong with this program?
#include <iostream>
#include <string>
using namespace std;
char abcde[30];
stupid.cc: In function `int main()':
stupid.cc:16: could not convert `&abcde' to `char&'
stupid.cc:8: in passing argument 1 of `int dash_to_underscore(char&)'
you're trying to pass a pointer (arrays are just fancy pointers).
with the call-by-reference method, you pass 'addresses' that can be modified by the function. what ever you changes you make to that object that was passed, is changed outside the function as well. essentially you are passing a pointer and are asking the function to use the address of an address :) ..you can change your function definition to
int dash_to_underscore(char * string)
and either pass the array you have...or you can use a char *, or even just pass the address of (&) a plain 'char'
[EDIT]
...sorry i was in the middle of posting :)
[/EDIT]
asking the function to use the address of an address
...sorry, i think i confused even myself with that statement :) ..because a pointer (in which the contents consist of an address), does infact, have its *own* address.
i think what i meant was...that the function, after receiving the object..then tries to manipulate that object using it's address. in the event that you should pass the pointer to that same function... first off ..it fails because a pointer of some_type is not a reference of some_type, in which case we have a type mismatch.. and second, if it *could* receive a pointer ...with its current implementation, the compiler would substitute that pointer with it's address which has stored in it, another address (for instance 0x1d03975 ...or whatever :) ..in which case, i believe we would be left manipulating that '0x1d03975' value ...in essence pointing the pointer to another area in memory (or just storing some illegal garbage value ), ...either way we destroy the pointer and/or just crash the prog.
...i think :)
i'm still learning all this myself...
if someone can correct anything that is wrong or *way* off with what i just said ...please do ...i would much enjoy *knowing* what is going on at all times....
Many thanks for the help, guys. Hope you don't mind me tacking on another couple of questions.
The book I'm using is from 1997. It seems that there has been some changes to C++ since then (e.g how the standard namespace is used and declaring iostream rather than iostream.h). I was thinking I could still work through the book I have and pick up on the changes as I go or afterwards. Do you think this is reasonable, or would I be better off ditching my book and getting a more up-to-date one?
Secondly, and maybe I'm jumping too far ahead of myself, but how can I tell what functions a class gives me (e.g. what else does iostream give me). And, coming from the other direction, if I need a function how can I find out what class it's in?
1: ...the only thing that getting a new book would hurt, is your wallet
2: get the source code for the c++ std library ..should've come with your distro on cd., or goto step one (above) ...give in ...get a book that covers that stuff in detail ..i have c++ programming langauge specail edition (stroustroup might as well have written the damn thing in russian) ...it's a good reference though ...also someone mentioned some ANSI/ISO c++ book , that may go into detail about that stuff ...but as far as 'browsing' the implementation is concerned, you better off just checking out the headers and source files i think ...IMO that is
The last two books I believe are two of the best books to have at hand for a C++ programmer.
The C++ FAQ is a very easy to read very informative book. The other covers almost the entire standard, but in a manner which is both readable and understandable by normal humans It covers the implementation details to great detail.
i'm going to go out on a whim and trust your judgement ..:)
i just ordered them :) ...it's a good thing you posted those links..i had a book that i was going to get on 65816 assembly (super nintendo(65c816) baby...OHHHH YEAH!!.) ..anyways , it was sitting in my shopping cart for like seven months (i forgot about it :), but i picked that up too. (that book is 'Programming the 65816: Including the 6502, 65C02, and 65802' for those that are interested in learning about the GREATEST SYSTEM MAN HAS EVER CONCEIVED!!! :) ...also useful for c64, and the atari used this processor family as well :) any nostalgia phreaks?
the book i got..'C++ Programming Langauge: Special Edition', isn't a baaad book persay. but it's not one of those books that you read from cover to cover, which is where i run into trouble. :) ,but i'm at the point now where i just try to get something done, and look through the book when i get stuck, and also for 'better' ways of doing something (like dakensta had posted in another post, using a multimap<>, which i found pretty cool)
either way...the more references the better i say.
so thanks again, especially for inadvertantly reminding me about my 'other' long lost book (it was printed in '79 and i've only seen it on amazon).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.