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
Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
To reduce the burden on the generic Programming Questions thread (and to increase the chance that people will actually answer questions), here is the C/C++ Programming Questions thread! I will assume you all know how these threads work: ask questions, or answer them. 'nuff said.

Feel free to submit useful links for the OP, I'll try to maintain them.

I want to start programming, is C (or C++) a good starting language?
C++ might be a little much to learn for someone just starting out, but even so, the language you start with doesn't matter. If you have a specific project in mind that requires C/C++, by all means use it, but don't feel that you need to start with a particular language in order to get the "most" out of learning. However, if you intend to learn both C and C++, it may be best to start with C, since C++ is designed to build on C, and most of the knowledge from C transfers to C++ (but not necessarily vice-versa).

gently caress! I'm on Windows and my program works fine for me, but no one else can run it!
Deployment on Windows is a bitch. With multiple versions of the C runtime library, debug DLLs, redistributables, and more, chances are you're not going to get it to work on your first try. The following links (thanks to Mustach) should at least point you in the right direction:

Visual C++ Deployment
How to redistribute the Visual C++ Libraries with your application



Useful Guides: (thanks in part to That Turkey Story)
The C++ Standards Committee
C++ FAQ Lite
SGI's STL documentation
Thinking in C++
The C++ Source
Overview of Generic Programming
Exception Safety

Libraries:
Boost
GLib

GUI Libraries: (thanks Professor Science!)
Windows API
GTK
Qt
wxWidgets

Good Books:
The C Programming Language by Kernighan and Ritchie
The C++ Programming Language by Bjarne Stroustrup
Accelerated C++ by Andrew Koenig and Barbara E. Moo
Other recommended C++ books

Avenging Dentist fucked around with this message at 05:58 on Dec 12, 2009

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

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

Milde posted:

This post outlines this problem.

Lack of partial template specialization for functions pisses me off so much. I needed it for a project, and ended up writing wrappers to do it (though I ultimately changed my design a little to avoid it). Maybe I should clean up my wrappers somewhat and submit them to Boost. :)

Avenging Dentist
Oct 1, 2005

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

Captain Frigate posted:

I'm pretty new to C and I am wondering what the actual difference is between putchar/getchar and printf/scanf. Is there any?

They are the exact opposites of each other. putchar and printf are for output, getchar and scanf are for input.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
This is less a question and more a comment, but I discovered the following "feature" in typedefs today (don't ask how):
code:
struct base
{
	struct foo { int a; };
};

struct sub : public base
{
	typedef foo bar;
	struct foo { int b; };
};

int main()
{
	sub::bar x;
	x.a = 10; // Good
	x.b = 10; // Won't compile, argh!

	return 0;
}

Avenging Dentist
Oct 1, 2005

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

JoeNotCharles posted:

Why the hell would it compile? x is a sub::bar, which is the same as a base::foo, so it doesn't have a b member. Only sub::foo, which is a completely unrelated struct, has a b member.

Well yes, that's the point. The example is greatly simplified; the original involved a complicated chain of template instantiations and was non-obvious, especially since it broke in a completely separate area (the typedef'd template was instantiated with base::foo and all subsequent references to that template, typedef'd or not, used that instantiation).

Most of it is kinda obvious in retrospect, but template metaprogramming has a tendency to make obvious things not-so-obvious. :)

EDIT: I am sure it doesn't help that I have been unable to concentrate on anything for the last couple months. :(

Avenging Dentist fucked around with this message at 06:38 on Feb 28, 2008

Avenging Dentist
Oct 1, 2005

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

That Turkey Story posted:

Are you saying that the base class was dependent on a template argument of the child?

Not exactly, though I should probably update Cygwin and run my stuff through gcc at some point. A more complete example of my problem would be:

code:
#include <map>
#include <string>

template<typename T>
struct collection
{
	typedef std::map<std::string,typename T::foo> type;
};

struct base
{
	struct foo { int a; };
};

struct sub : public base
{
	typedef collection<sub>::type collection_type;
	struct foo : public base::foo { int b; };
};

typedef collection<sub>::type collection_type;

int main()
{
	collection_type coll;
	coll["butts"].b = 10; // Failure! But would work if I commented out the
	                      // typedef in sub (or moved it after sub::foo)
	return 0;
}

Avenging Dentist
Oct 1, 2005

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

very posted:

I've been driving myself nuts for a few days just trying to set up a simple callback. I think I need some Barney-level help. I have two questions:

Firstly, this is the simplest kind of callback class that I have found:

code:
template < class Class, typename ReturnType>
class GUIButtonCallback < Class, ReturnType >
{
...

};
But this does not compile as-is. It will compile if I remove the < Class, ReturnType > after the class name, and I am not sure why that stuff is there anyway. It looks like a specialization, but why is it specializing in whatever the template specifies?

You can't specialize 0 of the template parameters, so it's not a specialization at all. :) (i.e. remove <Class, ReturnType>)

very posted:

code:
/* I don't want to specify a type here, because I have no idea what type 
of object this is going to be calling, I just want to be able to call anything! */
	GUIButtonCallback<?????, void> * m_callback;
With this scenario, is the callback class that I posted above even going to work? Should I just make it a void pointer? That seems to defeat the purpose and I don't know if that would work anyway.

Make a base class for GUIButtonCallback to inherit from and use virtual functions. That is,
code:
template<typename ReturnType>
class GUIButtonCallbackBase
{
	//...
};

template <typename Class, typename ReturnType>
class GUIButtonCallback : public GUIButtonCallbackBase<ReturnType>
{
	//...
};

class GUIButton : public GUIControl
{
	//...
	GUIButtonCallbackBase<void> * m_callback;
};

Avenging Dentist
Oct 1, 2005

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

Thug Bonnet posted:

On the other hand, seeking to the next newline and realloc()-ing the buffer for every line seems costly.

You'd only need to realloc when the next line is longer than the current one. If you pick a decent starting buffer length, there shouldn't be much reallocation.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Just use std::size_t. I'm not 100% sure what you're worried about, but size_t is perfectly acceptable, and it's not like the STL avoids it because they were too lazy to add another template argument. :)

Avenging Dentist
Oct 1, 2005

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

Lexical Unit posted:

Contrived example ahoy!

In that example, you'd want to use T::size_type. For merely counting the number of elements, (like .size() in STL containers), you'd use size_t. It's just a question of what exactly you want to measure.

EDIT: if you need both, you could have size_type and contained_size_type typedefs.

Avenging Dentist
Oct 1, 2005

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

cronio posted:

Not having the cast there is a compile error in C++ -- is that not true in C?

This is one of the important differences between C and C++. In C, void* can automatically be converted to any pointer type.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Jesus dude, that is some serious table breaking.

Also it looks like you're trying to define your template member functions in a separate compilation unit (.cpp file). C++ doesn't allow that. You need to define them in the header.

Avenging Dentist
Oct 1, 2005

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

floWenoL posted:

What is this "typing-time"?

It's the pre-preprocessing state, of course! :downs:

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
code:
class IntScheduler : public Scheduler<int>
{
    // ...
};

Avenging Dentist
Oct 1, 2005

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

very posted:

But I don't see it getting casted to CompoundPrimitiveManager * anywhere. Maybe I should stick a cast in and recompile the library?

Unless you're dealing with multiple inheritance, you shouldn't need to worry about casting.

Avenging Dentist
Oct 1, 2005

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

§3.9.1 #1 posted:

Objects declared as characters (char) shall be large enough to store any member of the implementation’s basic character set.
Probably the most direct answer from the standard.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Hopefully, in C++0x, there will just be an "iterator" concept. The more I think about concepts, the more I like them.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
"a.out" is an actual application. It's the default name in gcc for an executable if you don't specify one.

By "double click and open a little window", are you referring to a terminal (command line) window, or an actual GUI window? In the case of the former, what you have should already work. In the latter, you'll need to start learning a GUI API (no easy feat). Which API you pick depends on which platform(s) you want to develop for.

EDIT: the OP now has a list of GUI libraries, shamelessly stolen from Professor Science's reply on the first page. :)

Avenging Dentist fucked around with this message at 22:52 on Mar 24, 2008

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
As long as you're using a cross-platform API, the amount of rewriting you need to do should be minimal. wxWidgets, for instance, uses native controls and supports a wide variety of operating systems.

Avenging Dentist
Oct 1, 2005

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

BraggPxnk posted:

I have heard a lot about how Java is a lot like C++ and I always considered learning it.

They have a lot of syntactical similarities, and to a total beginner, they're pretty similar, but when you start getting into more advanced things, they can be very different, especially in terms of design philosophy.

Avenging Dentist
Oct 1, 2005

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

CasualCliffy posted:

but according to the standard, you cannot declare a template parameter to be a friend.

I don't see anything in the standard that says this, except that default template parameters can't be used in friend template declarations.

Avenging Dentist
Oct 1, 2005

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

That Turkey Story posted:

If you really want this, put the implementation of your class into a private base, then, from inside Foo, pass off that base to T so that it may access the implementation.

It looks like Comeau will let you do something like what he wanted with C++0x extensions on. Did the standard change in this regard?

Avenging Dentist
Oct 1, 2005

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

Lexical Unit posted:

Do you think it would be better to just leave out all the conditional compiling and just flatly require boost?

Just require Boost. Speaking of, have you been following the Trie discussion on the Boost developers list?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Be warned, you'll be inundated with mail unless you check your mail everyday. The Boost developer's list is incredibly busy. I should set up some more filters for it.

Avenging Dentist
Oct 1, 2005

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

Plastic Jesus posted:

I came across this a year or two ago:

That is possibly the stupidest thing I've read this year. Are you sure he wasn't trolling?

Avenging Dentist
Oct 1, 2005

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

Plastic Jesus posted:

Yes, Rob Pike is an idiot.

Just because he's a famous programmer doesn't mean that he's right. Besides, like people have said, compilers are quite a bit smarter than they used to be, and the performance difference should be negligible (especially compared to template instantiation :xd:), while the maintenance difference is huge.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Is there something wrong with doing this?
code:
switch(foo)
{
case 1:
    // blah
    break;
default:
    return false;
}

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Is there a way to truncate a stream at a certain offset? That is, suppose I have a file open and want to delete a line of text and shorten a file by so many bytes. I can shift the subsequent lines of text with no problem, but there'll be extra space at the end, which kind of defeats the purpose in this case.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
It's pretty much essential that this work with stream objects, since the only guarantee I have is that the stream is open for writing, and supports random access. Nine times out of ten, it'll be a file, but it's entirely possible that the file will be run through a Boost.Iostreams filter like gzip.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
You can use objects in templates, that's the point. You're trying to call insertFront with a reference as the parameter and not a pointer. The types need to match up.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Don't bother with casts, just do fwrite("\n",1,1,fd);

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Oh, I forgot to mention, another option would be to use fputc('\n',fd); if you're only writing single characters.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Even if it did work, wouldn't you end up indirectly specializing base::x instead of making a specialized version just for derived::x? Also, I'm pretty sure explicit specializations aren't allowed inside a class.

The following is probably closer to what you want (compiles under GCC, Comeau, and VC++2005):
code:
struct base
{
	template <typename T>
	struct x;
};

template <>
struct base::x<float>
{
	typedef float some_type;
};

struct derived : base
{
	template<typename T>
	struct x : base::x<T>
	{};
};

template <>
struct derived::x<int>
{
	typedef int some_type;
};

int main()
{
	derived::x<int>::some_type i;
}

Avenging Dentist
Oct 1, 2005

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

lhunc posted:

Another C newb question, this time regarding the passing of structures to functions.

..

Can anyone point me in the right direction?

If you're doing pure C, you need either struct note in place of note, or the following:

code:
typedef struct { /* ... */ } note;
note notes[1000];

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Could you just post your code on https://www.pastebin.com or something?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
operand is a single character and should be a char*. I'm not sure why you're using atof to parse a single character though...

Also, you aren't allocating any memory for *number.

EDIT: haha

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Er, does that even compile?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Bender: "... and I thought I saw a two!"
Fry: "It was just a dream, Bender! There's no such thing as two."

Avenging Dentist
Oct 1, 2005

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

Zombywuf posted:

For some reason it seems CObject is not EqualityComparable, except privately.

That's the assignment operator, but the rest of your post still stands.

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Have you looked at Boost.GIL (Generic Image Library for C++)?

http://www.boost.org/doc/libs/1_35_0/libs/gil/doc/index.html

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