Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Richard M Nixon
Apr 26, 2009

"The greatest honor history can bestow is the title of peacemaker."
I'm trying to do some homework that is covering overloaded functions with default arguments, and I'm a bit confused. My professor has me write two functions
code:
void PrintOutput (int, int);
void PrintOutput (int, int, int);
for displaying the information for either a rectangle or a triangle. He has now asked me to create two addidional DEFAULT arguments that will specify cout or fout, and short or long output. I understand that to write this, I would have
code:
void PrintOutput (int, int, ostream &out = cout, bool short = true);
void PrintOutput (int, int, int, ostream &out = cout, bool short = true);
to have the defaults be a short output to the screen, but he wants me to provide for the screen output and file output to be short or long by themselves (short screen and long file, for example). How would I call, for example, an output to the screen that is long? He wants me to "use the mininum number of arguments necessary" for the call. Am I able to call
code:
PrintOutput (num1, num2, ,false);
or must I call
code:
PrintOutput (num1, num2, cout,false);
thus reiterating (technical word for this?) the default argument?

Thanks!

Adbot
ADBOT LOVES YOU

Fart Sandwiches
Apr 4, 2006

i never asked for this
I am currently learning C and I have a quick question.

I want to use a system call to print the date command from within my program.

My code looks like this:

code:
printf("%s", system("date"));
And the output looks like this:

code:
Mon Jul 20 13:32:37 EDT 2009
(null)
I know how to get rid of the newline by using:

code:
printf("%s", system("date | tr -d '\n'"));
But how do I get it to stop printing (null)? I'm pretty sure it's something to do with how printf() handles the %s deal, but I just have no clue. If I use tr -d '\0' I get a segmentation fault. Any suggestions?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

chips posted:

I'm not sure how you'd generically copy the argument lists, though.

code:
// TODO: specialize for void return type
template<typename Class, typename Ret, typename... T>
std::function<Ret (T...)> decorator(Class *self, Ret Class::*fcn(T...))
{
    return [self, fcn](T&&... t) { return self->fcn(t...); };
}
Or something like that anyway. You could probably eliminate std::function, which would help performance, but I don't care enough to do that for an example.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

xwonderboyx posted:

I want to use a system call to print the date command from within my program.

Get rid of the printf entirely. Just call system.

RjY
Apr 24, 2003

system() just executes date which prints the time itself. system() then returns 0 which gets converted into a null pointer which printf prints as "(null)"

You need to use ctime(time(NULL)) or something.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

xwonderboyx posted:

Any suggestions?

Use the standard date time functions available in <time.h>.

system("date") prints a date, it doesn't return a date.

Richard M Nixon
Apr 26, 2009

"The greatest honor history can bestow is the title of peacemaker."
EDIT: I messed up, the edit button and quote/reply button are not the same thing

Richard M Nixon fucked around with this message at 18:57 on Jul 20, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Now you've gone and done it. I'm just going to have to recommend Boost.Parameter. :colbert:

Fart Sandwiches
Apr 4, 2006

i never asked for this

ShoulderDaemon posted:

Get rid of the printf entirely. Just call system.

This is the answer that works best for me! I appreciate all the other help as well, but for what my current assignment is I didn't want to use anything in <time.h>

Thanks!

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

livelikecode posted:

thus reiterating (technical word for this?) the default argument?

Usually people would say "providing".

Obviously this is for a class, and you have to do what your professor wants, but as a quick note — you should really never use overloading in conjunction with default arguments in real code.

xwonderboyx posted:

But how do I get it to stop printing (null)?

You should be compiling with warnings on.

EDIT: removed redundant answers

rjmccall fucked around with this message at 18:56 on Jul 20, 2009

Bhaal
Jul 13, 2001
I ain't going down alone
Dr. Infant, MD

livelikecode posted:

Am I able to call
code:
PrintOutput (num1, num2, ,false);
or must I call
code:
PrintOutput (num1, num2, cout,false);
thus reiterating (technical word for this?) the default argument?

Thanks!
You would want the 2nd one, but you might find it easier (if i read your post correctly) if you reorder the default arguments, putting the args that are least likely to get changed at the end:
code:
void PrintOutput(int, int, bool short = false, ostream &out = cout);
void PrintOutput(int, int, int, bool short = false, ostream &out = cout);
And then example calls:

PrintOutput(2,4,true);
PrintOutput(3,1,2,false,fout);
PrintOutput(3,2,3);
etc...

If his real true goal is to explore the world of overloading (which I think it is, because the 2-int / 3-int overloading is very questionable as is), and if he really wants you to use minimal output for each call (ie. you'll be graded on it), then you'd keep the functions in your post and include these as well. So you can do retarded poo poo like this:

// short and cout(default)
PrintOutput(1,2,true);

// fout and long(default)
PrintOutput(1,2,fout);


But like I said it might serve as a good exercise for seeing what you can do with overloading, but in practice: readability >>>> # of arguments.

EDIT: I'm dumb and forgot the compiler throws a fit with that level of ambiguity, so it's probably best to just reorder the arguments so that the defaults that get changed the most (if you can make that distinction) are closer to the front.

EDIT2: oh ya, short is a keyword so you'll have to rename that bool :haw:

Bhaal fucked around with this message at 22:49 on Jul 20, 2009

Fecotourist
Nov 1, 2008

Avenging Dentist posted:

Now you've gone and done it. I'm just going to have to recommend Boost.Parameter. :colbert:

More like Boost.Parentheses (Boost.ParlorTrick?). If the example code for your library ends up being that ugly, just pat yourself on the back for being too clever by half and leave it at that.

newsomnuke
Feb 25, 2007

I'm trying to create a makefile which has targets for both a static and a shared library. My rule for source files is just:
code:
.cpp.o:
        $(CXX) $(CXXFLAGS) -c $< -o $@
which works for static, but I need to use -fPIC for shared. What's the best way of doing this?

Contero
Mar 28, 2004

I've been using C++ pretty intensely for the last year and for some reason I don't hate it. Clearly this is because I don't know enough about it!

So what are the more advanced areas of C++ that I probably don't know? I know basic templates, but I look at some of the boost libraries and I'm amazed at the things people have managed to create from within the language.

What do I need to learn to go from being ok in C++ to being really good?

Zombywuf
Mar 29, 2008

Contero posted:

I've been using C++ pretty intensely for the last year and for some reason I don't hate it. Clearly this is because I don't know enough about it!

So what are the more advanced areas of C++ that I probably don't know? I know basic templates, but I look at some of the boost libraries and I'm amazed at the things people have managed to create from within the language.

What do I need to learn to go from being ok in C++ to being really good?

Read Herb Sutter's Exceptional C++.

Foiltha
Jun 12, 2008

Contero posted:

I've been using C++ pretty intensely for the last year and for some reason I don't hate it. Clearly this is because I don't know enough about it!

So what are the more advanced areas of C++ that I probably don't know? I know basic templates, but I look at some of the boost libraries and I'm amazed at the things people have managed to create from within the language.

What do I need to learn to go from being ok in C++ to being really good?

Read Effective C++ by Scott Meyers.

http://www.amazon.com/Effective-Specific-Addison-Wesley-Professional-Computing/dp/0201924889

TSDK
Nov 24, 2003

I got a wooden uploading this one
Also read:

The C++ Standard Library - Josuttis
C++ Gotchas - Dewhurst
C++ Common Knowledge - Dewhurst
C++ Coding Standards - Sutter & Alexandrescu
C++ FAQ - Cline
C++ Templates the Complete Guide - Vandervoorde & Josuttis

And online:

Guru of the Week - http://www.gotw.ca/gotw/

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Just FYI, I work on a C++ compiler in my spare time, and I have read precisely one "book" on C++: the ISO standard. All of those other books are completely unnecessary in my opinion. I learned everything by writing code and looking at other people's code.

Fart Sandwiches
Apr 4, 2006

i never asked for this
This is pretty silly and goes back to my question earlier about using system() to call date.

I was tasked with writing a program that received data from a server that then computes an MD5 hash on the data so that the hash on the data I got matched the hash on the data that the server sent. I then had to format the output on my end to match the server end, which needed to look something like this:

code:
Date and Time                |IP Address       |Port #    |MD5SUM
-----------------------------------------------------------------
Sat May 16 06:50:43 EDT 2009 |192.168.11.128   |44604     |*MD5 hash*
My original code was something like this:

code:

printf("Date and Time\t\t\t|IP Address\t|Port #\t|MD5SUM\n");
printf("----------------------------------------------------");
system("date | tr -d '\n'"); /*the tr -d '\n' is so the next printf will follow the date*/
printf("\t|%s\t|%d\t|", inet_ntoa(in), ntohs(ClntAddr.sin_port);
system("md5sum file"); /*computes md5sum on data I received earlier that I happened to save to a file*/
The output from this would be:

code:
Date and Time                |IP Address       |Port #    |MD5SUM
-----------------------------------------------------------------
Sat May 16 06:50:43 EDT 2009 dhajkas8sa7Gllsdfk90a8nhdhlaslkj
               |192.168.11.128   |44604     |
The output ended up with date and the md5 hash directly next to each other with the printf statement that is between them appearing on the next line. This was puzzling so I performed some wizardry and eventually got the output to look like it does at the beginning of this post.

However, upon looking over my code my supervisor told me there are better ways to do what I did even though system() is the easiest, yet most dangerous. He then told me to find out how the system function works and why it screwed up my output and then tell him by the end of the day. My google skills are apparently lovely because I can't find a real explanation as to why it was screwing with my output.

I can imagine this doesn't really make sense, but there you have it.

TSDK
Nov 24, 2003

I got a wooden uploading this one

Avenging Dentist posted:

I learned everything by writing code and looking at other people's code.
That may be true, but you also learned a seething hatred for all other programmers and in fact human life itself in the process ;)

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

xwonderboyx posted:

My google skills are apparently lovely because I can't find a real explanation as to why it was screwing with my output.

The printf functions "print" to an internal buffer in your process's memory space, which is only sent to the output stream occasionally. system runs a new process, which obviously has its own buffers and consequently appears to your program to output "immediately". So even though you had a printf between your system calls, it wasn't really taking effect until after both of them.

Fart Sandwiches
Apr 4, 2006

i never asked for this

ShoulderDaemon posted:

The printf functions "print" to an internal buffer in your process's memory space, which is only sent to the output stream occasionally. system runs a new process, which obviously has its own buffers and consequently appears to your program to output "immediately". So even though you had a printf between your system calls, it wasn't really taking effect until after both of them.

Thank you!

This thread is too awesome.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
If you are using a bunch of system calls, why are you even writing this in C? Wouldn't a shell script make more sense?

Fart Sandwiches
Apr 4, 2006

i never asked for this

Avenging Dentist posted:

If you are using a bunch of system calls, why are you even writing this in C? Wouldn't a shell script make more sense?

I'm in the military and the way they want you to learn C is to go to a week long class and then hand you a sheet of paper with C COMPETENCY written on it. You then learn by doing. I did it, and that's what matters.

Zombywuf
Mar 29, 2008

xwonderboyx posted:

I'm in the military and the way they want you to learn C is to go to a week long class and then hand you a sheet of paper with C COMPETENCY written on it. You then learn by doing. I did it, and that's what matters.

Please tell me you have nothing to do with missiles.

Fart Sandwiches
Apr 4, 2006

i never asked for this

Zombywuf posted:

Please tell me you have nothing to do with missiles.

I,uh, have nothing to do with missiles.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
If you're hell-bent on using system calls, at least use popen instead and then read from the FILE* returned from it.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Avenging Dentist posted:

Just FYI, I work on a C++ compiler in my spare time, and I have read precisely one "book" on C++: the ISO standard. All of those other books are completely unnecessary in my opinion. I learned everything by writing code and looking at other people's code.
I agree. It's too easy to fall into the trap of reading a lot about programming but never actaully doing any. The other side of this, though, is you need to know a priori what code is worth reading and worth learning from.

A code reading thread with suggestions could actaully be interesting and enlightening.

taqueso
Mar 8, 2004


:911:
:wookie: :thermidor: :wookie:
:dehumanize:

:pirate::hf::tinfoil:

Dijkstracula posted:

A code reading thread with suggestions could actaully be interesting and enlightening.
I think this would be a very interesting thread. I was just going to head over to the python thread to ask about recommended well-structured python programs to check out.

Zombywuf
Mar 29, 2008

Dijkstracula posted:

I agree. It's too easy to fall into the trap of reading a lot about programming but never actaully doing any. The other side of this, though, is you need to know a priori what code is worth reading and worth learning from.
Well I think it's worth reading a lot and writing a lot. It's only by attempting to use stuff you've read about that you can tell whether or not it's stupid. However reading about code saves you having to discover everything yourself.

However, reading code is usually pretty dull. The best code is boring, the hard part is making it boring.

chips
Dec 25, 2004
Mein Führer! I can walk!
It might be problematic for the reason that it's easy to show examples of bad code but hard to show examples of good code (especially concise examples that aren't entire projects). That said, CoC Code Review could be interesting.

Zombywuf
Mar 29, 2008

chips posted:

It might be problematic for the reason that it's easy to show examples of bad code but hard to show examples of good code (especially concise examples that aren't entire projects). That said, CoC Code Review could be interesting.
Now there's a good idea, if we weren't all such bitter, cynical, critical bastards.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
What's the point? If you (the reader) can't tell when code is good, you're not going to gain anything by someone showing you code that they believe is good, unless you're seriously arguing that you should write code using the monkey-see-monkey-do pattern.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Avenging Dentist posted:

What's the point? If you (the reader) can't tell when code is good, you're not going to gain anything by someone showing you code that they believe is good, unless you're seriously arguing that you should write code using the monkey-see-monkey-do pattern.
This is where the wiser among us would pipe in and say, "For instance, I like this program's code (see source files foo.c and butt.c) because of __, __, and __, but I'm not fond of __ and __ (see source files foo.c and butt.c)" to kick-start the discussion.

Contero
Mar 28, 2004

Avenging Dentist posted:

What's the point? If you (the reader) can't tell when code is good, you're not going to gain anything by someone showing you code that they believe is good, unless you're seriously arguing that you should write code using the monkey-see-monkey-do pattern.

*flips through GoF* I don't see the monkey-see-monkey-do pattern?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Contero posted:

*flips through GoF* I don't see the monkey-see-monkey-do pattern?

It's the whole book. :pwn:

Battle Bott
May 6, 2007

I for one would love to hear about why code is good or bad.

Example:
http://www.urubatan.info/2008/11/commenting-source-code-is-only-for-the-weak/

The first example looks perfectly fine to me, and the refactored version is more confusing.

Anyway, my problem:
I'm parsing a midi file, and I have a MidiFile class that has a member variable that is a list of Track objects, and each Track object has a list of TrackEvent objects ("Events").

When I do this:
code:
string Track::Print() {
	stringstream S;
	S << "Begin Track:" << endl;
	PrintEvent=Events.begin();
	while(PrintEvent!=Events.end()) {
		S << PrintEvent->Print();
		PrintEvent++;
	}
	S << "End Track." << endl << endl;
	return S.str();
}
It works just dandy.

When I try to iterate through the list over time it doesn't work:
code:
TrackEvent* Track::CurrentEvent() {
	return &*CurEvent;
}
TrackEvent* Track::NextEvent() {
	if(isLastEvent()==false) {
		CurEvent++;
	}
	return &*CurEvent;
}
Any time I call CurrentEvent() outside of where the list iterator CurEvent is defined (Track constructor) it doesn't work. I don't modify the list, and a debugger shows me that the TrackEvent object it is pointing to is missing half of it's member variables.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Battle Bott posted:

I for one would love to hear about why code is good or bad.

Example:
http://www.urubatan.info/2008/11/commenting-source-code-is-only-for-the-weak/

Pretty sure that article is a troll. If you write a function that is only called once (and you never intend to use it other places), you are an idiot. Doubly so if that function is a one-liner. Triply so if the function name is almost as long as the code inside the function. Quadruply so if you misspelled the function name.

Also, what are you even talking about in your question?

BattleMaster
Aug 14, 2000

Is it okay to use one-shot functions if you want to avoid several 100+ line sections within a switch-case block? If not then I'm clearly an idiot.

That article rules though.

Adbot
ADBOT LOVES YOU

Battle Bott
May 6, 2007

Hah, forgot to finish it. Reading a perl tutorial, sorry.

When I try to call CurrentEvent() I get a runtime error "list iterator not dereferancable"

This line would fail:
code:
cout << Midi->Tracks.begin()->CurrentEvent()->Print();
Assuming Midi is a MidiFile object.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply