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
Allie
Jan 17, 2004

Lexical Unit posted:

How do I specialize method templates of class templates? :downs:

I might be wrong, but I don't think C++ allows partial specialization of member function templates. Maybe something like this would work for you instead?
code:
template<typename T>
struct foo {
    // ...
    template<typename S>
    void bar(S l);

    void bar(int l);
};

// ...

template<typename T>
void foo<T>::bar(int l) {
    cout << "an int: " << l << endl;
}
This post outlines this problem.

Allie fucked around with this message at 05:02 on Feb 18, 2008

Adbot
ADBOT LOVES YOU

Allie
Jan 17, 2004

freddy-10eighty posted:

I'm currently taking an intro to C++ at my uni.... can anyone tell me how to compile and run C++ files on a mac? I've tried using XCode, and it's a bit overwhelming since I'm still new to this...basically, when I write code, compile and build in MS Visual studio, a console window pops up and allows me to test out my program, but the same thing doesnt happen with XCode...nothing pops up.

Open up Terminal.app (in /Applications/Utilities) and use g++ to compile your application. Much simpler than dealing with a huge IDE and hundreds of project settings.

And don't forget the -Wall -Wextra switches. :)

Allie
Jan 17, 2004

Super Dude posted:

How can I add elements to the beginning of a vector? I essentially want to merge two vectors of different length, putting one ahead of the other.

a.insert(a.begin(), b.begin(), b.end());

That will insert all the elements from b into the beginning of a.

Allie
Jan 17, 2004

Super Dude posted:

If I have a vector 'alphabet', and I want to erase a single element at index j, is this the correct syntax?

alphabet.erase(j,1);

There's erase(iterator pos) and erase(iterator first, iterator last). The latter erases a range of elements. You want the former (and with an iterator).

You should bookmark this http://www.sgi.com/tech/stl/Vector.html. :)

Allie
Jan 17, 2004

Yes, because you need an iterator, not an int. If you really want to do it by index, try alphabet.erase(alphabet.begin() + j). But if j is just a loop counter, you should be using an iterator instead of a counter.

Allie
Jan 17, 2004

Donald Duck posted:

Segmentation faults don't give line numbers... And I was just editing the result in as you printed that, sorry.
Thanks, knew it was something like that.

If you're using gcc, you can use the -g switch which includes debugging info. If you run the program in gdb, or load a core dump from a debug build in gdb, it'll tell you the line numbers, and it'll even print snippets of the code if it can read the file from the path you compiled it from.

I believe MSVC has a similar feature you can use if you compile a debug build and run the application through the IDE.

Allie
Jan 17, 2004

vanjalolz posted:

Can someone explain what I'm doing wrong with semaphores here: http://codepad.org/4MXCzn9o

On OSX it compiles, but on fedora it fails to link:

sem.c:(.text+0x59): undefined reference to `sem_wait'
sem.c:(.text+0x8e): undefined reference to `sem_post'


You probably need to tell GCC to link to it with -lpthread. OS X shoves a lot of POSIX functions into one huge library (libSystem) which is probably why it worked there.

Also, your code doesn't actually compile for me with gcc 4.1 (due to type assignment/argument errors), but I'm guessing you just need to link to that library. I would install the dev man pages for your distribution and make sure you're using the API correctly.

Allie fucked around with this message at 06:44 on May 25, 2008

Allie
Jan 17, 2004

I'm pretty sure that value gdb prints each time is the return value of the function. printf doesn't return a string, it returns the amount of characters it wrote (an int). You probably aren't seeing it actually print anything because you aren't including a newline, so stdout isn't getting flushed.

Also, it probably doesn't make sense to print curr_q as a string since it looks like its first member isn't a string. You should be using the proper data types in your format strings. %i (or more commonly, %d) refers to a signed integer.

Basically, you need to read the documentation for the libraries you're using, including glib's documentation and the standard library documentation for your distribution. You also might want to read gdb's documentation. :)

Allie
Jan 17, 2004

You should read the documentation for something if you aren't familiar with it or haven't used it in a while. Man pages will describe return values. If you're using a function and it's returning something that doesn't make any sense to you, it's probably a good idea to review the documentation before you jump to conclusions.

Your second example calls printf without newlines, which means the output won't necessarily show up in your console immediately. And the second call to printf passes curr_q as a string, even though it's a struct. I wasn't commenting on your first example, and I said it doesn't make sense to print the struct as a string, not that it's irrelevant.

I don't know why you're getting different results in and out of the debugger, but sometimes it is possible for this to happen. Were those debugger commands run postmortem? Have you tried loading the core dump the program generated when it crashed?

If you're really absolutely stumped, I recommend running valgrind on your program. It might reveal the source of the problem, or reveal that something else is indirectly causing the crash. Turning on all/extra warnings in your compiler might also help.

Allie fucked around with this message at 04:38 on Jun 14, 2008

Adbot
ADBOT LOVES YOU

Allie
Jan 17, 2004

Mustach posted:

If you view the source in Firefox, it shows you the source as modified by FF, not the original.

Actually, this is only true if you view the source of a selection.

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