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
If you mean http://cimg.sourceforge.net/, then you can use GetObject to get the bitmap data and then pass it to the CImg constructor.

Adbot
ADBOT LOVES YOU

Jo
Jan 24, 2005

:allears:
Soiled Meat

Randall posted:

I just bought Accelerated C++ by Koenig/Moo today. I'm not even on chapter 2 and am already lost.

I consider myself fairly knowledgeable in computer tech but I have no experience in any programming language whatsoever. I decided this would be something I'd like to learn and didn't expect it to be easy but, jeez, so much of the jargon used in the book goes over my fragile little mind. :psyduck:

It's been a long while since I started programming, so take this with a giant spoon of salt. The best way to become proficient in a programming language is to use it. Build tiny applications -- simple things like

Hello World.
Hello (name).
A program that takes a text file and adds line numbers.
A simple calculator.
etc

This will teach you something beyond, "Okay public static void main here," and into the realm of understanding WHY things are being done the way they are.

Lallander
Sep 11, 2001

When a problem comes along,
you must whip it.

Jo posted:

This will teach you something beyond, "Okay public static void main here," and into the realm of understanding WHY things are being done the way they are.

Been hitting the Java a little too hard have we?

Rahu
Feb 14, 2009


let me just check my figures real quick here
Grimey Drawer
Looking for a little basic c++ help here, google was insufficient.

Is there some standard way of handling template functions properly? The only way I've found to properly implement

code:
template <class T>
T returnInput(T input) {
    return input;
}
is to write the function definition in the header file. Is there some more proper way to do this or should I abandon all hope of leaving real code out of headers?

Vanadium
Jan 8, 2005

Rahu posted:

should I abandon all hope of leaving real code out of headers?

This, I am afraid.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Templates always go in headers unless you have a compiler that supports export (I can just about guarantee you don't).

For further reading, see N1426.

(tl;dr: even the people who managed to implement export - it wasn't easy - said it's not worth it)

TSDK
Nov 24, 2003

I got a wooden uploading this one

Rahu posted:

Is there some more proper way to do this or should I abandon all hope of leaving real code out of headers?
A style I've been trying to pimp for a while is this:
code:
// MyTemplate.h

template < typename T >
void MyTemplateFunction();

#include "MyTemplate.hpp"
code:
// MyTemplate.hpp

template < typename T >
void MyTemplateFunction()
{
    // blah
}
So you end up with a h/hpp pair for template classes and functions that has roughly the same declaration/definition split as with h/cpp non template classes and functions.

Doesn't seem to have caught on so far, but I like it.

functional
Feb 12, 2008

Parsing in C

I've been programming for a long time, and I know C, but I am not a C master. Most of my knowledge on how to manipulate strings is from higher-level languages, and there I'm pretty good at it. In C I'm a bit clunkier, which is bad, because I'm about to write a heavy-duty parser. It would help me to look at some code that shows how a pro would do things. Should I be using free() and strndup() a lot to extract tokens? How should I go about classifying different types of tokens? What's the best way to read essentially the same way from a file or from stdin?

spiritual bypass
Feb 19, 2008

Grimey Drawer

functional posted:

What's the best way to read essentially the same way from a file or from stdin?

Is IO redirection an option? I like it.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender
If you're doing parsing in C, you should first be able to justify why lexx+yacc or a derivative won't work for your situation; they are a lot more robust than building a parser yourself.

functional
Feb 12, 2008

ShoulderDaemon posted:

If you're doing parsing in C, you should first be able to justify why lexx+yacc or a derivative won't work for your situation; they are a lot more robust than building a parser yourself.

OK, I will look into that. In the meantime I'm still going to need to know how to do these things myself, since I won't be able to rely on lex/yacc for everything.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

functional posted:

OK, I will look into that. In the meantime I'm still going to need to know how to do these things myself, since I won't be able to rely on lex/yacc for everything.

Well, the acts of tokenizing input, creating a state table, and managing all of the minutiae of parsing input are really irritating and difficult to implement well in C. It's a terrible language for it. Lex and yacc can pretty much always do the hardest parts for you, and there are very few good reasons to not be using them; in cases where they won't work for you, knowing how to build regex tokenizers and LALR parsers in C won't help you, because what you want is something else at that point.

floWenoL
Oct 23, 2002

ShoulderDaemon posted:

Well, the acts of tokenizing input, creating a state table, and managing all of the minutiae of parsing input are really irritating and difficult to implement well in C. It's a terrible language for it. Lex and yacc can pretty much always do the hardest parts for you, and there are very few good reasons to not be using them; in cases where they won't work for you, knowing how to build regex tokenizers and LALR parsers in C won't help you, because what you want is something else at that point.

Well, if you're going to be implementing a parser in C the only practical option is a recursive-descent parser.

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


What happens if a function that's supposed to return something fails to return?

code:
CRawData* getData()
{
  if (foo)
  {
    return bar;
  }
  // note the conspicuous lack of return here
}
Is it A) returns nothing and the lvalue being assigned to by this function is unaffected; B) returns total loving garbage and said lvalue is wiped out; or C) totally undefined?

Brief googling says C, and that's what I suspect as well, but I just want to confirm.

And holy gently caress Sun CC can lick my balls, this is an important thing to warn about at maximum warning level you loving prick! :argh:

Dijkstracula
Mar 18, 2003

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

Ledneh posted:

Brief googling says C, and that's what I suspect as well, but I just want to confirm.
Indeed; whatever happens to be on the stack will be what gets returned.

Wow, that's certainly an overlooked compiler warning.

Avenging Dentist
Oct 1, 2005

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

Dijkstracula posted:

Wow, that's certainly an overlooked compiler warning.

Warnings for something like that can only be emitted properly if the compiler supports something like the noreturn function attribute. Otherwise, this would generate a warning:

code:
int my_func()
{
    if(valid)
        return 42;
    else
        abort();
}
Even then it would require dataflow analysis to determine more complicated instances.

Avenging Dentist fucked around with this message at 00:01 on Feb 21, 2009

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


On the one hand, I'm HUGELY relieved that I found this because it's going to fix a lot of problems down the road (beyond the one I just solved). On the other, holy gently caress I am STUNNED that Sun CC doesn't emit a warning at any level when not all control paths return. I thought that was kind of a fundamental thing to watch out for.

(edit) On the THIRD hand, I could just find the guy who wrote that piece and throttle him. I bet I know who it is too. Argh.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

ISO C++ Standard posted:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

So that's (C). With POD types, you'll usually just get an undefined value of some sort, which of course will generally lead to undefined behavior if used.

Pedantic comment: returning values via the stack is the exceptional case on most platforms, not the norm.

julyJones
Feb 12, 2007

Stopped making sense.

functional posted:

OK, I will look into that. In the meantime I'm still going to need to know how to do these things myself, since I won't be able to rely on lex/yacc for everything.

You might start with strtok() ("string tokenizer".) You'll probably find other helpful stuff in "string.h" too.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Note that strtok is generally not thread-safe.

Dijkstracula
Mar 18, 2003

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

Speaking of string functions, I'm porting a side project that I started working on in OSX to Linux and I see that the strlcpy/strlcat functions don't appear to be in Interpid's glibc...any idea why this might be the case? Is this just an Ubuntu thing, or is it not to be found anywhere in Linuxdom? At school we had "STRLFOO(), NOT STRNFOO(), AND CERTAINLY NOT PLAIN OLD STRFOO(), YOU IDIOTS" beaten into us, and those machines were all running Linux, so maybe the functions were added in after the fact...?

I'd make a disparaging comment about Linux, but I think I'll save that for YOSPOS.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Dijkstracula posted:

Speaking of string functions, I'm porting a side project that I started working on in OSX to Linux and I see that the strlcpy/strlcat functions don't appear to be in Interpid's glibc...any idea why this might be the case? Is this just an Ubuntu thing, or is it not to be found anywhere in Linuxdom? At school we had "STRLFOO(), NOT STRNFOO(), AND CERTAINLY NOT PLAIN OLD STRFOO(), YOU IDIOTS" beaten into us, and those machines were all running Linux, so maybe the functions were added in after the fact...?

I'd make a disparaging comment about Linux, but I think I'll save that for YOSPOS.

Linux normally does not have those functions, because some primary glibc developers believe that they can lead to unnoticed string truncation bugs and are not particularly safer than the *n* functions. Certainly if you use the *n* functions correctly there are no risks of buffer overflows, and it's no harder than using the *l* functions correctly; the *l* variants only cover you if your code mixes usage of the *l* functions and the completely unsafe variants.

Sabotaged
Jul 6, 2004

More on template functions, as the earlier discussion on this page reminded me that I came across this today: What's the purpose of the template<> in this?

template<>
static const std::string tostr(const std::wstring& s)
{
return std::string(s.begin(), s.end());
}

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
That code is invalid.

(Which is to say it might work, but it's almost certainly not what you want. Use a regular function.)

Avenging Dentist fucked around with this message at 10:30 on Feb 21, 2009

Vanadium
Jan 8, 2005

Dijkstracula posted:

"STRLFOO(), NOT STRNFOO(), AND CERTAINLY NOT PLAIN OLD STRFOO(), YOU IDIOTS"

What does strlfoo do? :shobon:

Great Britain
Dec 17, 2005

& Northern Ireland
I have an enumeration with these values in (something tells me it would chose those values but default anyway, but I didn't want to risk complicating it even more for now).

code:
enum letters {A=0,B=1,C=2,D=3,E=4,F=5,G=6,H=7,I=8,J=9,K=10,L=11,M=12,N=13,O=14,P=15};
Then I assign a letter to fish and send it to TestSquare's enumeration
code:
char fish;
cin >> fish;
TestSquare(grid,fish,2);
code:
void TestSquare(int grid[16][16], letters lx, int y)
The program crashes and the other ways I've tried don't work either. Is there a way I can get input from the keyboard and set the enumeration to that?

Standish
May 21, 2001

Injured Zebra posted:

Is there a way I can get input from the keyboard and set the enumeration to that?
Not directly, no. You could mess with the ASCII codes and do:
code:
TestSquare(grid,fish - 65,2);
but that's kind of ugly. Better to write something like the following to map all your input to valid enum values:
code:
letters inputToEnum(char ch)
{
  if ((ch >= 65) && (ch <= 80))
  {
    return static_cast<letters>(ch - 65);
  }
  else
  {
    // input out of range -- error handling
  }
}

Great Britain
Dec 17, 2005

& Northern Ireland

Standish posted:

Not directly, no. You could mess with the ASCII codes and do:
code:
TestSquare(grid,fish - 65,2);
but that's kind of ugly. Better to write something like the following to map all your input to valid enum values:
code:
letters inputToEnum(char ch)
{
  if ((ch >= 65) && (ch <= 80))
  {
    return static_cast<letters>(ch - 65);
  }
  else
  {
    // input out of range -- error handling
  }
}

I'll try that thanks.

Vandorin
Mar 1, 2007

by Fistgrrl
I've got a question about making your own class. I know I need a header file, and a .cpp file, and I have written both of those out, what I was wondering was, do I put the main part of the program in the same .cpp file or do I just go to add->new etc, and insert the code for the main part of my program there?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Do whatever you want.

oldkike
Jan 10, 2003

hey

www.pleasegimmeadollar.com

Vandorin posted:

I've got a question about making your own class. I know I need a header file, and a .cpp file, and I have written both of those out, what I was wondering was, do I put the main part of the program in the same .cpp file or do I just go to add->new etc, and insert the code for the main part of my program there?

The most commonly used pattern I've seen is adding a main.cpp to the project with nothing more than a main function.

mistermojo
Jul 3, 2004

How do I strip an extension from a file in C?

For example, I have a file called "output/1.png". I can use basename to get it to "1.png". I want to remove the .png as well.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

mistermojo posted:

How do I strip an extension from a file in C?

For example, I have a file called "output/1.png". I can use basename to get it to "1.png". I want to remove the .png as well.

Either use index(3) or rindex(3) to find the period, then set it to NUL. You need to decide if you want "foo.bar.baz" to turn in to "foo.bar" (use rindex(3)) or "foo" (use index(3)).

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

ShoulderDaemon posted:

Either use index(3) or rindex(3) to find the period, then set it to NUL. You need to decide if you want "foo.bar.baz" to turn in to "foo.bar" (use rindex(3)) or "foo" (use index(3)).

index(3) and rindex(3) are both part of strings.h rather than string.h and thus aren't going to be present on a non-POSIX system (even then, both are marked as 'legacy' in POSIX.1-2001). You're better off using something like strchr(3)/strrchr(3) or even strtok(3) if you care about portability (which mistermojo may or may not).

e: forgot about strrchr

Blotto Skorzany fucked around with this message at 03:50 on Feb 23, 2009

ehnus
Apr 16, 2003

Now you're thinking with portals!
strrchr should work, too.

Cirrus_Alreia
Oct 26, 2007
Count Chocula Incarnate
In C++ is there any way to do if(x[f][b] == int)?
I just need it to know if the place is an integer so that it can run through the statement.
Is there any way to do it?[google searches have led nowhere]

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!

Cirrus_Alreia posted:

In C++ is there any way to do if(x[f][b] == int)?
I just need it to know if the place is an integer so that it can run through the statement.
Is there any way to do it?[google searches have led nowhere]

Depending on what x is, you may be looking for function overloading, templates, typeof (not part of the standard) or someone to hit you on the fukken head.

Cirrus_Alreia
Oct 26, 2007
Count Chocula Incarnate

Painless posted:

Depending on what x is, you may be looking for function overloading, templates, typeof (not part of the standard) or someone to hit you on the fukken head.
jesus christ.
all I need to know is there some simple way to deduce whether x is of type int.

Vanadium
Jan 8, 2005

Cirrus_Alreia posted:

jesus christ.
all I need to know is there some simple way to deduce whether x is of type int.

Check the declaration of x, then.

Adbot
ADBOT LOVES YOU

That Turkey Story
Mar 30, 2003

Cirrus_Alreia posted:

jesus christ.
all I need to know is there some simple way to deduce whether x is of type int.

You probably are approaching your problem the wrong way and should be branching out via overloads as was already recommended. If you are very stubborn here or are sure that the functionality you are asking for really is the most elegant solution, you can use BOOST_TYPEOF or make a ghetto typeof facility:

code:
char is_int(...);
char (&is_int(int))[2];

#define IS_INT( value ) ( sizeof( ::is_int( value ) ) == 2 )
Edit: Also, keep in mind that if you are in a template, a simple if branch will still force the code to be compiled for other types, it will just never be reached. You will have to branch off using compile-time facilities if the statements in the branch only compile for ints.

That Turkey Story fucked around with this message at 15:39 on Feb 23, 2009

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