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
JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Is your log class flushing the stream? If you're watching the log file in realtime, waiting for the lines to show up, they might just be buffered (which would happen too in your stubbed test case, except the program ends right after the last call which flushes all the buffers).

Adbot
ADBOT LOVES YOU

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Avenging Dentist posted:

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;
}

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.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Avenging Dentist posted:

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).

Oh, I thought the "argh" was "argh C++ is so stupid why won't it let me do this" and not "argh I am so stupid how did I gently caress this up".

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Peanutmonger posted:

Simple makefile?! Maybe at first, but if you were really into it, you'd go all out and use autotools. Vim (especially with ctags/etc) and autotools make a fantastic development environment.

You're insane. Autotools are ridiculously terrible to work with.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Peanutmonger posted:

I probably am. I also probably haven't used autotools well enough to get out of the "honeymoon" phase of how wonderful it is that configure will discover everything for me and build a Makefile with all of the targets I've come to expect. I really enjoyed doing a "make dist" and copying that to my other desktop and building it.

Using stuff that's already written to use autotools is great, yeah. Setting up your project to use autotools is a huge, huge pain in the rear end.

I like SCons myself, though I haven't used at enough to find the inevitable flaws.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

WalletBeef posted:

What is the best, free, windows C++ compiler?

As far as I know, your choices are basically Visual Studio Express or MinGW. Express has the advantage that it's closer to the full, for-pay version of Visual Studio, which is what all Windows software ever is built with, but the disadvantage that it's missing some things and lots of documentation or other software you might want to work with assumes you have those things. MinGW is the Windows port of gcc, the compiler used on every OS except Windows, which is completely free itself, so documentation about gcc will usually transfer right over.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

ColdPie posted:

I have a question about inheritance in C++. The C++ FAQ says that inheritance is not for code re-use, but rather for polymorphism and other related features. This seems to conflict with the old "Shape" example (class Shape contains a Position method and related variables, Circle and Rectangle inherit from Shape and inherit this method and data, so more code re-use). Is using inheritance for this reason wrong? If so, why? What am I missing here?

The point is that Position and related variables logically belong to all the various subclasses of Shape. If you had a method that happened to be coded very similarly for Circles and Rectangles, but not for Squares, you wouldn't want to add a base class that both Circle and Rectangle inherit from but Square doesn't just to hold this method, because that would make no sense. You also wouldn't put the Circle and Rectangle version in the base class and then have Square override it, because the Circle and Rectangle version doesn't apply to all Shapes so it has no reason being in the base class. (You also wouldn't just duplicate the code in the two classes - make a standalone friend function that's called from both, or something.)

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

MEAT TREAT posted:

Visuacl C++ 2008 Express Edition

It has the added benefit of being able to create nice GUI's.

Uh, Visual C++ doesn't have a monopoly on nice GUI's. I'm not even sure what you mean by that - a compiler that couldn't be used to compile a GUI is pretty useless.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

GT_Onizuka posted:

I have never worked with C++ or autotools, and I'm getting super frustrated. I have no idea how easy/difficult this is, but when I have more time, I want to make it much easier to compile (like, ./configure && make && make install easy). If it's a larger problem than I currently think, I'm more than willing to pay a bit, considering the urgency of this.

It's almost that simple already. Install automake and autoconf, then:

code:
make -f Makefile.dist && ./configure && make && make install
Makefile.dist is a KDE-ism which you probably wouldn't have known to look for. Really, would it have been that hard for them to put the two lines about in the INSTALL or README files?

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Mornacale posted:

e: Problem solved. Thanks. Pay no more attention to the C++ n00b behind the curtain.

The guy below you already posted, but in general, people, if you solve the problem without they board's help, please post the solution! Other people might be wondering what it was.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

very posted:

Edit: Actually maybe I have no idea what is going on. Is "this" as it shows up in the debugger supposed to change? Is it showing me the address of this, or something else? I went back to my old implementation and this never changes in any function.

Edit 2: Why are these pointers "expression cannot be evaluated"? They were just newed.


While in a method of an object, "this" is supposed to point to the instance that's currently executing the method. If it's pointing to invalid memory, something is very, very wrong. (If you're not in an object method, it'll just be undefined.)

It's expected to change as soon as you enter a new object. For example, in the following code if you step into each function call, it'll change as you enter the functions and be restored when you come back out. If you're not entering another function, it shouldn't change.

code:
SomeClass::someMethod()
{
  // "this" should point to the current instance of SomeClass
  object1->func();  // while in this method, "this" will point to object1
  object2->func();  // in this method, "this" will point to object2
  // "this" should be back to the SomeClass instance
}
Your other problem is a direct consequence of this one: in this contedxt m_world is a shortcut for this->m_world, and if "this" is invalid the whole thing is.

The most common reason for "this" to be invalid is if you call a method using a bad pointer, but this would only give you one single invalid value (and it usually crashes pretty quickly). If it keeps changing to a bunch of different bad values, something very weird is going on.

code:
delete obj1;
... some time later
obj1->func();
Depending on compiler and circumstances, this might crash right there, or get quite a ways into "func" with "this" set to whatever random memory obj1 is pointing to before bad things happen.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

schnarf posted:

I've got a class inheritance question. I'm writing a scheduler using templates, so that I'll have a generic framework for the scheduler, and the actual process function is virtual so that I can write a variety of schedulers for whatever datatype and scheduling algorithm I want. This is for an assigment. My question is, the schedulers that I implement, inheriting from my base class Scheduler<T> will not need to be templated; they'll use only one datatype because the algorithm will be specific to that datatype. How do I specify that the inherited class isn't templated, but rather uses a specific datatype?

Were you required to use both templates and inheritance in this assignment? Because it seems way more complicated than it's worth.

"Schedular<T>" is not a class. It's a family of classes, "Schedular<int>", "Schedular<string>", "Schedular<SomeOtherClass>", etc. So you have to pick just one individual class (ie. just one instantiation of the template) to inherit from:

code:
class IntegerSchedular : public Schedular<int>
{
  // method overrides
};

class StringSchedular : public Schedular<string>
{
  // method overrides
};
IntegerSchedular and StringSchedular aren't templated. But you can't take advantage of polymorphism this way, because IntegerSchedular and StringSchedular don't share any common base class! So you can't create a function that takes a "Schedular" and then pass it either IntegerSchedular or StringSchedular, because there's no such thing as just a Schedular.

One way to get around that is

code:
class SchedularBase
{
  // methods that don't depend on the type T, if any
};

class Schedular<T> : public SchedularBase
{
  // methods that do depend on the type T
};

class IntegerSchedular : public Schedular<int>
{
  // etc
};
Now you have SchedularBase as a base class of all schedulars - but one of the more important things a base class is used for is to enforce contracts (interfaces), and SchedularBase isn't very useful for that because some of the interface will depend on the type T and be declared in the templated class.

In summary, rethink this. You probably need generics OR inheritance, not both.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
A char is not an object.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

BraggPxnk posted:

drat, I was hoping there would be something a little easier than learning an API but oh well.

I was going to use it cross platform because my main computer is a Mac and all of my friends run windows. I wanted to make an 'application' so that I could make a little program just messing around and send it to them to use/make jokes about/help me improve my programming, stuff like that.

Well, I'll just start checking out some of these sites. Thanks.

I'm a big fan of Python + Qt, although if one of the goals of this is to improve your C++ programming skills, just plain C++ and Qt would be more appropriate.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

ColdPie posted:

What would you suggest? It includes common utilities, constants, and structs that are used in a whole bunch of sources. What's the alternative? I'm seriously asking here, I would love to avoid that mess in future projects.

Is the mess causing any concrete problems? Makefiles slow due to dependency analysis, etc? If not, don't bother trying to avoid it - the compiler's perfectly capable of dealing with crap like that. It's not 1989 anymore.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

ColdPie posted:

Absolutely not, it works fine. It just feels messy and "bad designy" like he mentioned. If it's normal, all right, just doesn't seem like the best way to do things.

Well, one thing is that you're doing a global dependency track. That's always going to be both messy and useless. Look at each individual C++ file and you'll see something that looks a lot more like a plain tree for each (until they join together at that one file that's included by everyone).

In fact, even for the global imposing some order on that graph will help a lot. It looks like it was drawn with graphviz behind the scenes - graphviz's default format always leads to messy looking graphs. If you assigned ranks to the nodes so that all .cpp files were at the bottom and the layout favoured vertical rather than horizontal arrangements you'd get a much more pleasing lattice.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Sarah Sherman posted:

Holy poo poo, after looking over this code I understand (vaguely) what is happening and what is going on in each section of the code, but I definitely do not have the programming skill to actually write a program like this. Thanks a lot for the help and discussion guys, but it looks like I'm going to have to scrap this project until I know more. Once I think I have the skills to actually write this program, then I'll for sure speak up again if I need some help. This thread is a great resource, and I enjoy keeping up with this thread (and this subforum in general).

If you're just doing this for yourself, and not a school project, try Python. The major structure of the program will be the same, but you won't have to spend so much time worrying about C syntax and arcane function naming.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

TurtleBoy posted:

I am looking into programming a game and I had a question which google is proving useless to answer, possibly because I am asking it the wrong questions.

The art assets for this project I want to store in such a way that only the program can retrieve them(the artists are VERY protective of their work). By this I mean how most games you play don't have big folders of jpgs for everything, they have some sort of data file that is read when loading a level.

What is the best way to implement this?

Point out to the artists that no matter how much work you spend hiding the art, people will still be able to take screenshots of it.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Captain Frigate posted:

Can anyone see anything wrong with these function declarations? I keep getting "previous declaration of 'readFlights' was here" and "previous declaration of 'print_flight' was here" when I try and compile it.

code:
void readAirports(FILE *, airport airports[100]);
void readRoutes(FILE *, route routes[500]);
void readFlights(FILE *, flight flights[3000], int count[3000]);
void print_flight(flight flights[3000], int count[3000], route routes[500], airport airports[100]);
void printairport(route routes[500], airport airports[100]);
What do those errors even mean?

It means you're including declarations of those functions more than once, and the different declarations don't match. The most obvious way is if you accidentally have an older version in a different header file or something. I see you use a lot of custom types (airport, route, etc.) so, assuming that's in a header file, it could be you're including the header file twice and redefining one of those types in between.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Does your header include #ifndef guards?

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Captain Frigate posted:

No. Could that be what is causing the problem?

No, I was just wondering. :rolleyes:

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Captain Frigate posted:

I had no idea what that was before you brought it up and I searched for it. Some people don't already know a language back and forth.

The point is, why would I have asked (not to mention I'm the second person to bring it up) if it wouldn't be causing the problem?

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Try boost::enable_if

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

lhunc posted:

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

I've got a structure like this:
code:
struct note {
    time_t creation_date;
    int priority;
    int *note_text;
} notes[100];

As a general principle, you're probably better off splitting this into a declaration of a struct type, and a declaration of an array of that type.

code:
struct note { ... };
struct note notes[100];
It's just less confusing that way.

quote:

and then a function which takes an array of note structures, which I'm sure is wrong...
code:
void get_notes(note *some_notes);

This is one of the areas where C and C++ differ. In C, it's

code:
void get_notes(struct note *some_notes);
You use struct note { ... }; to declare a structure, and just struct note to use it.

Since this is really annoying, generally you take that structure and typedef it to an easier name.

code:
struct note_struct { ... };
typedef note_struct note;
note notes[100];

void get_notes(note *some_notes);
Since the "struct and then typedef" idiom is so common, people usually combine it into one block, like you did with the array declaration - which is why you shouldn't do that with array declarations, because at a quick glance people will assume it's a typedef. (I was mighty confused for about 10 seconds why you were naming your note type "notes[100]".)

code:
typedef struct note_struct { ... } note;
note notes[100];

void get_notes(note *some_notes);
EDIT: oh yeah, you can just cut the word note_struct completely, create an anonymous struct, and then typedef it to the name "note". Been so long since I did C that I forgot that.

code:
typedef struct { ... } note;
note notes[100];

void get_notes(note *some_notes);

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Did you declare that struct in a header file? Are you sure you're not including an older version of the header or something?

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

MEAT TREAT posted:

Thanks for the tips Plastic Jesus. The reason that loop is so weird is because I'm supposed to give a 0 when they guess the right color but it's not in the correct position and a 1 when it's the right color and the right position.

Also I had never thought about calloc returning a null pointer. What is the best course of action in that case? I can't just exit the server because the client would be left hanging for who knows how long.

The best thing to do is have a message the server sends the client to tell it that it's being shut down, and preallocate all memory needed to send that message at the beginning of the program. Then if calloc ever returns null you can send that last message before shutting down without having to allocate more memory.

It's not that terrible to shut down and leave the client hanging, though, since the client should be able to deal with timeouts anyway (what happens if the network goes down?) - better than trying to forge ahead without memory.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Just pretend they're 7 digits wide instead of 8. So instead of 0,8,16,24, the sequence is 0,7,14,21

0 * 2^24 + 0 * 2^16 + 2 * 2^8 + 1 * 2^0 = 513 (wrong)
0 * 2^21 + 0 * 2^14 + 2 * 2^7 + 1 * 2^0 = 257 (right)

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Yeah, but I didn't even notice the 2 made no sense, so I fail at computer science.

I assume it was supposed to be "00000010 00000001"

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
If you're trying to save it to a file to print, I don't understand why you're even trying to display it on the screen. Use an imaging library, not a GUI one. (I don't know enough about them to know which can handle really large images well, though.)

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Scaevolus posted:

Look up line-drawing algorithms. You could initialize a 12GB raw image file with zeros, then draw lines by doing file seeks and writes. It wouldn't be pretty, and it might take a very long time, but with good filesystem caching it might be manageable.

That's the worst idea I've ever heard. The man is doing VECTOR GRAPHICS.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
That's definitely better than "i < 10000000" because it's checking against an actual time instead of just an arbitrary counter, so it works the same no matter how fast your processor's running. The counter method is completely broken; this is just unoptimal.

What you want is "sleep" (I think it might be _sleep in Visual C++, nut that shouldn't matter to you). Problem is it only takes whole seconds, so the lowest you can do is "sleep(1);" to sleep for 1 second. If that's ok, this is the easiest.

If that's not good enough, you can use "nanosleep" (which I think is Unix-only), which takes a duration measured in nanoseconds but it's a bit more complicated because you need to fill in a structure instead of just passing an int. Google should be able to tell you how to use it.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Avenging Dentist posted:

There's also usleep, which takes microseconds. All of these should be in unistd.h

usleep isn't available everywhere, though - nanosleep and sleep are the most portable. Probably doesn't matter in this case, but it's a good idea to get in the habit of using nanosleep.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Recursion-wise that looks right - the only problem is "4(x)" won't compile, it's "4*x".

ColdPie posted:

Just kind of at a first glance (about to run out the door to school myself), this looks all right. I'd add in a check to make sure x isn't negative -- that'd create an infinite loop. Depending on exactly how the requirements are worded, I'd throw the check for <=0 at the start:

This is always a good idea, and falls under "always check for unexpected inputs". The function will never get down to a negative number while it's recursing, but it'll break if someone else calls "recurse(-1)". The easiest and best fix is to change it to "unsigned int recurse(unsigned int x)" so it's not even possible to pass it a negative number.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Since this is C++ code, why doesn't LongPacket just inherit from Packet so you can use static_cast?

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Paniolo posted:

Also you can't declare an array like this:

code:
int x = 5; // or whatever
int array[x];

Actually you can in gcc - it's a compiler extension. (You shouldn't, though, because it makes your code non-portable.)

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Smackbilly posted:

This may be silly/stupid, but is it in any way possible to do this?

code:
class Foo : public Bar<Foo::Things::Thing> {

public:

  struct Things {
    enum Thing {
      // ...
    };
   };

   // ...

};

Off the top of my head, try

code:
class FooBase {
public:
  struct Things { ... };
};

class Foo : public Bar<FooBase::Things::Thing>, public FooBase {
  ...
};
Since Foo inherits FooBase, I believe you can now refer to FooBase::Things as Foo::Things. (Not in the actual definition of Foo, but at least you're containing the inelegance.)

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Professor Science posted:

What the gently caress, Qt 4.4 supports futures? Holy poo poo.

I took a look for that and couldn't find any docs. Can you point me to a reference?

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Oh, I completely missed that 4.4 already had an entry on the main doc page - I was looking around for previews and getting pissed off that all the Google links were outdated.

Wow, the whole QConcurrent framework looks fantastic.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Vanadium posted:

Or you could just use boost::add_pointer<int>::type foo, bar; to avoid any confusion or ambiguity.

I love me some boost, but that's disgusting.

Adbot
ADBOT LOVES YOU

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

vanjalolz posted:

Ha Ha what the hell? Do Boost pointers actually add anything useful to warrant such terrible syntax?

It's meant to be used in contexts where there isn't any other syntax. You're not supposed to use it in standard situations.

For instance, in a template function you want to say, "Whatever type the parameter is, get a pointer to that type."

code:
void foo<typename T>(T t) {
  boost::add_pointer<T>::type tPtr = &t;
  ...
}

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