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
Smackbilly
Jan 3, 2001
What kind of a name is Pizza Organ! anyway?

shodanjr_gr posted:

What the heck am I doing wrong? The rest of the code compiles just fine


edit:
Compiling and linking the class separetely seems to have fixed this...Still if someone can explain why it happens in the first place...

Depends on how you were trying to compile it before (as in, what command line did you use, including any other source files?).

Adbot
ADBOT LOVES YOU

Bitruder
Nov 29, 2003

Majoring in connect the dots and colouring
I'm using g++ on linux and I have Boost installed in $HOME/local64 (libs are in ./libs and includes are in ./include/boost).

When I compile I need to add -L $(HOME)/local64/lib to the linker flags in order for it to compile (else I get the error:
code:
/usr/bin/ld: cannot find -lboost_thread-gcc43-mt
Is there a way to get ld to always search there? I tried setting LD_LIBRARY_PATH to $HOME/local64/lib but even when I do that, I still get the same error.

Any ideas?

Bitruder fucked around with this message at 19:12 on Dec 27, 2008

Nippashish
Nov 2, 2005

Let me see you dance!

Bitruder posted:

I'm using g++ on linux and I have Boost installed in $HOME/local64 (libs are in ./libs and includes are in ./include/boost).

When I compile I need to add -L $(HOME)/local64/lib to the linker flags in order for it to compile (else I get the error:
code:
/usr/bin/ld: cannot find -lboost_thread-gcc43-mt
Is there a way to get ld to always search there? I tried setting LD_LIBRARY_PATH to $HOME/local64/lib but even when I do that, I still get the same error.

Any ideas?

I would suggest making yourself a Makefile. Stick all the non-standard paths you need to search in there and then forget about it. This also has the nice side effect of documenting your strange dependencies.

Bitruder
Nov 29, 2003

Majoring in connect the dots and colouring

Nippashish posted:

I would suggest making yourself a Makefile. Stick all the non-standard paths you need to search in there and then forget about it. This also has the nice side effect of documenting your strange dependencies.

Yes, I use a Makefile. The thing though, is that I want to distribute my source, so the -L flag would be specific to my computer instead of being more general.

whoknew
Sep 18, 2004


oh shit
im lit



Bitruder posted:

Yes, I use a Makefile. The thing though, is that I want to distribute my source, so the -L flag would be specific to my computer instead of being more general.

If you create a configure script with autoconf, you have the ability to pass the script a prefix for your libraries and such, so that could be a good solution. And it's definitely not a bad idea to include a configure script if you're planning on distributing your source, because other people might have weird paths too.

covener
Jan 10, 2004

You know, for kids!

Bitruder posted:

I'm using g++ on linux and I have Boost installed in $HOME/local64 (libs are in ./libs and includes are in ./include/boost).

If you haven't blown away the implicit rules in your makefile, set CXXFLAGS/LDFLAGS in your environment or pass it on the command-line to make.

PT6A
Jan 5, 2006

Public school teachers are callous dictators who won't lift a finger to stop children from peeing in my plane
I'm a programmer with a fairly decent command of C, and enough Java and Python that I'm familiar with general OOP techniques as well. Is there a good book that introduces C++, while not going over the basics of C at the same time (or, even better, one that specifically highlights the differences between the two)?

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->
Is stringstream only supposed to be used for numerical values? I'm trying to do a single line of input for 2 strings and 2 integers, by doing something like:

code:
string input, str1, str2;
int var1, var2;

getline(cin, input);
stringstream(input) >> str1 >> var1 >> str2 >> var2;
but my compiler balks at me with:
code:
error: no match for 'operator>>' in 'stringstream(((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)
((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(&instr))), std::operator|( _S_out,  _S_in)) >> cmd'|
I had this working in my last project with just two integers, so I'm wondering if stringstream is only for numerical values, or if maybe they just have to be of the same data type. I'm trying to avoid cin >> str1 >> var1 >> str2 >> var2 because I am supposed to exit my input loop by entering "quit", and nothing else, which does not work correctly with that method.

Cosmopolitan fucked around with this message at 00:25 on Dec 29, 2008

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
I'm too lazy to verify this, but I think you need to make that stringstream a named variable
code:
string input, str1, str2;
int var1, var2;
stringstream ss;

getline(cin, input);
ss.str(input);
ss >> str1 >> var1 >> str2 >> var2;
operator >> takes a non-const stream and temporaries are const.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
I'm pretty sure temporaries are non-const but can't be bound to non-const references due to the fact that it's an r-value.

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

Mustach posted:

I'm too lazy to verify this, but I think you need to make that stringstream a named variable
code:
string input, str1, str2;
int var1, var2;
stringstream ss;

getline(cin, input);
ss.str(input);
ss >> str1 >> var1 >> str2 >> var2;
operator >> takes a non-const stream and temporaries are const.

Hmm, this compiles with no errors, but it's not extracting any values from the input. :confused:

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Then you're doing it wrong.

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

Avenging Dentist posted:

Then you're doing it wrong.

Oh, okay, just realized I didn't do a cin.ignore for a cin that happens before my getline. It's working now. Thanks a bunch. :)

On that note, if someone is willing, I'd like to better understand in what circumstances you'd need to use cin.ignore, and for what reason. This way, I will just know when to use it, rather than using it as a quick fix for when the input gets funky.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Formatted input doesn't read delimiters after the input (e.g. newlines) so you have to pop off the newline if you want to use getline.

slovach
Oct 6, 2005
Lennie Fuckin' Briscoe
How the poo poo do I allocate memory for a struct array like this with HeapAlloc?

code:
	struct{
		float	x,
			y,
			z,
			size;
	}stars[NUM_OF_STARS];	
obviously the idea is to allow changing of it's size, instead of hardcoding it.

chglcu
May 17, 2007

I'm so bored with the USA.

slovach posted:

How the poo poo do I allocate memory for a struct array like this with HeapAlloc?

code:
	struct{
		float	x,
			y,
			z,
			size;
	}stars[NUM_OF_STARS];	
obviously the idea is to allow changing of it's size, instead of hardcoding it.

If you really want to use HeapAlloc for some reason:
code:
// star structure
struct Star {
    float   x,
            y,
            z,
            size;
};
...
// initial allocation
Star * stars = (Star *)HeapAlloc(GetProcessHeap(), 0, sizeof(Star) * initialStarCount);
...
// resizing
stars = (Star *)HeapReAlloc(GetProcessHeap(), 0, stars, sizeof(Star) * newStarCount);
...
// releasing memory when done
HeapFree(GetProcessHeap(), 0, stars);
If instead you want to use the standard C library functions
code:
// initial allocation
stars = (Star *)malloc(sizeof(Star) * initialStarCount);
...
// resizing
stars = (Star *)realloc(stars, sizeof(Star) * newStarCount);
...
// releasing memory when done
free(stars);
Best bet would probably be to use a vector, unless you're using C and not C++
code:
// initial allocation
std::vector<Star> stars (initialStarCount);
...
// resizing
stars.resize(newStarCount);
...
// releasing memory happens automatically when stars vector goes out of scope
Also, I believe if you're just using C and not C++, the casts in the first two examples can be removed, and you'll also have to use "struct Star * stars" and "sizeof(struct Star)" instead of just Star. At least I think that's how it works in C, been forever since I've done pure C programming.

Bitruder
Nov 29, 2003

Majoring in connect the dots and colouring
Is there a quick and reliable way to check if X11 is available? My application is (Linux of course) a CLI application that generates EPS images. If X11 is available, I'd like to launch gv image.eps to show the user, but if it's not available, I won't bother. This isn't a compile check since users could be running this off their desktop or maybe just SSH'ing in.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Bitruder posted:

Is there a quick and reliable way to check if X11 is available? My application is (Linux of course) a CLI application that generates EPS images. If X11 is available, I'd like to launch gv image.eps to show the user, but if it's not available, I won't bother. This isn't a compile check since users could be running this off their desktop or maybe just SSH'ing in.

The standard technique is to check whether the DISPLAY environment variable is set, though I would suggest also adding a command-line option to enable/disable/configure this feature.

rjmccall fucked around with this message at 21:54 on Dec 29, 2008

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

prolecat posted:

Also, I believe if you're just using C and not C++, the casts in the first two examples can be removed,
Correct, a void pointer can be implicitly cast to any pointer type in C

prolecat posted:

and you'll also have to use "struct Star * stars" and "sizeof(struct Star)" instead of just Star.
You can get around this pretty simply with a typedef, either
code:
typedef struct{
     float  x,
            y,
            z,
            size;
} Star;
or
code:
typedef struct star Star;
struct star{
     float  x,
            y,
            z,
            size;
};
or several other similar ways depending on preference and whether you need to forward-declare things, google is your friend in that case.

Mr.Radar
Nov 5, 2005

You guys aren't going to believe this, but that guy is our games teacher.
I have a question about the lifetime of stack objects that are r-values in C++. Say I do something like the following:
code:
struct ConvertString {
    wchar_t *temp;
    ConvertString(const char *input) {
        temp = new wchar_t[strlen(input)+1];
        /* convert input and store in result in temp */
    }
    ~ConvertString() {
        delete[] temp;
    }
    operator wchar_t *() { return temp; }
};

extern void bar(wchar_t*);

void foo(const char *someString) {
    bar(ConvertString(someString));
}
What is the lifetime of the ConvertString object instantiated in calling bar? The behavior I'm looking for is that it's constructed, implicitly converted to wchar_t* through the conversion operator, bar is called, bar returns, then the object is destructed. g++ 4.3.2 exhibits that behavior, but I was wondering if the standard said anything about this kind of situation or if the behavior I saw was otherwise reliable and expected with most compilers.

Mr.Radar fucked around with this message at 02:42 on Dec 30, 2008

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
12.2.3. Temporary objects are destroyed as the last step in evaluating the full-expression that lexically contains the point where they were created.

The two exceptions are when initializing reference (12.2.5) and non-reference (12.2.4) variables, both of which only extend the lifetime of the object. So gcc's behavior is, in fact, mandated by the standard.

Mr.Radar
Nov 5, 2005

You guys aren't going to believe this, but that guy is our games teacher.
Thanks! That makes things a lot easier for me.

shodanjr_gr
Nov 20, 2007
Got a simple one for you C++ goons:

How can I create a two-dimmensional array of pointers to objects of class Foo?

Vanadium
Jan 8, 2005

shodanjr_gr posted:

Got a simple one for you C++ goons:

How can I create a two-dimmensional array of pointers to objects of class Foo?

http://www.boost.org/doc/libs/1_37_0/libs/multi_array/doc/user.html#sec_introduction

shodanjr_gr
Nov 20, 2007
Thanks for the reply. I am trying to avoid using another library if possible (plus I want to refresh my C++ skills particularly with regard to pointers and stuff).

Actually, I think i figured it out and it goes something like this:

code:
Foo ***myArray = new Foo**[someSize];
for(int i = 0; i < someSize; i++)
{
myArray[i] = new Foo*[someOtherSize];
for(int j = 0; j < someOtherSize; j++)
{
myArray[i][j] = new Foo();
}
}
//and to access
std::cout<<(*myArray[2][66]);

}

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
That's not a two dimensional array to Foo-pointers, that's a pointer-to-array of pointers-to-arrays of Foo-pointers.

shodanjr_gr
Nov 20, 2007

Avenging Dentist posted:

That's not a two dimensional array to Foo-pointers, that's a pointer-to-array of pointers-to-arrays of Foo-pointers.

Is there a large functional difference between the two?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
2d arrays are rectangular, arrays of pointers-to-arrays are jagged.

Also I am required to tell you that your code is extremely exception-unsafe.

shodanjr_gr
Nov 20, 2007

Avenging Dentist posted:

Also I am required to tell you that your code is extremely exception-unsafe.
I'll live with that for now. Thanks for the info :)

I got another one.

code:
class foo
{
public:
static const foo A_STATIC_MEMBER_OF_FOO;

foo(); //default constructor

//rest of the class
}

const foo foo:A_STATIC_MEMBER_OF_FOO; //initialization


foo::foo(){} //default constructor
and then somewhere else

code:
foo& myFunction()
{
return foo::A_STATIC_MEMBER_OF_FOO; //this gives a compile error
}
How can I code the function so I can return a reference to A_STATIC_MEMBER_OF_FOO, while at the same time retaining the ability to return a reference to a normal Foo instance? (edit: The error I get is: Invalid Initialization of reference of type 'Foo&' from expression of type 'const Foo'. Also I added the initialization statement, sorry about that).

shodanjr_gr fucked around with this message at 01:11 on Dec 31, 2008

floWenoL
Oct 23, 2002

shodanjr_gr posted:

code:
Foo& myFunction()
{
return Foo::A_STATIC_MEMBER_OF_FOO; //this gives a compile error
}

Hey guys, time for another round of "Guess the problem without the error message"!

(My guess: you don't actually define Foo::A_STATIC_MEMBER_OF_FOO anywhere (the code you have just declares it).)

newsomnuke
Feb 25, 2007

You're declaring your static member as const but trying to return a non-const reference to it.

Nippashish
Nov 2, 2005

Let me see you dance!
You're also trying to put a foo inside a foo, which is not possible.

Avenging Dentist
Oct 1, 2005

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

Nippashish posted:

You're also trying to put a foo inside a foo, which is not possible.

Is your mind blown???

floWenoL
Oct 23, 2002

Avenging Dentist posted:

Is your mind blown???

:O

Jo
Jan 24, 2005

:allears:
Soiled Meat
I'm having a strange problem with std::deques.

code:
void legalizeTriangle( Triangle * t, deque <Triangle*> triangulation ) { /* blah */ }

list <Triangle*> triangulate( list <Point*> points ) {
	/* blahblahblah */
	legalizeTriangle( trianglesToCheck.at(i), triangles );
}
Which is freaking out. Running gdb,

Before legalizeTriangle, triangles.size() is 3. I step into legalizeTriangle and the size of triangles is 67896278964243840598345

code:
220                     Point * points = triangulation.at(i)->getPoints();
(gdb) print triangulation.size()
$7 = 1161059713442302
(gdb) up
#1  0x0000000000445bf1 in triangulate (points=
            {<std::_List_base<Point*, std::allocator<Point*> >> = {_M_impl = {<std::allocator<std::_List_node<Point*> >> = {<__gnu_cxx::new_allocator<std::_List_node<Point*> >> = {<No data fields>}, <No data fields>}, _M_node = {_M_next = 0x7fff4e88e9e0, _M_prev = 0xc2c450}}}, <No data fields>}) at delaunay.h:301
301                             legalizeTriangle( trianglesToCheck.at(i), triangles );
(gdb) print trianglesToCheck.size()
$8 = 3
(gdb) print triangles.size()
$9 = 2
I'm not throwing any pointers around. Why would this happen?

EDIT: For clarity, the size changes AS SOON as I step into the function. It's as if passing the stl container changes the values inside.

Jo fucked around with this message at 20:55 on Jan 1, 2009

shrughes
Oct 11, 2008

(call/cc call/cc)

Jo posted:

:words:

What the gently caress are you talking about? Learn how to communicate properly.

Edit: Your compiler's broken.

Edit: Why don't you isolate the problem in some demonstratory program, so that people can actually tell you what's wrong, rather than saying "Hey, you're right, that looks funny."?

shrughes fucked around with this message at 21:29 on Jan 1, 2009

Jo
Jan 24, 2005

:allears:
Soiled Meat

shrughes posted:

What the gently caress are you talking about? Learn how to communicate properly.

Edit: Your compiler's broken.

Edit: Why don't you isolate the problem in some demonstratory program, so that people can actually tell you what's wrong, rather than saying "Hey, you're right, that looks funny."?

Noted. I thought the GDB excerpt was self explanatory. I'll try to be more clear.

Also, working on an isolated test case.

beuges
Jul 4, 2005
fluffy bunny butterfly broomstick
Don't you want to make LegalizeTriangle take a const reference rather than make a copy?

code:
void legalizeTriangle( Triangle * t, const deque <Triangle*>& triangulation ) { /* blah */ }

Jo
Jan 24, 2005

:allears:
Soiled Meat

beuges posted:

Don't you want to make LegalizeTriangle take a const reference rather than make a copy?

code:
void legalizeTriangle( Triangle * t, const deque <Triangle*>& triangulation ) { /* blah */ }

Goddamnit. :doh:

Works now. Thank you.

Adbot
ADBOT LOVES YOU

BigRedDot
Mar 6, 2008

It should still work the other way, though. Copying isn't the real problem, it merely somehow exposed the real problem... Maybe you previously smashed your stack (and still are) and making this local copy was the final straw, maybe your compiler is busted, who knows, there just isn't enough information here to guess. Personally, I wouldn't be content that the real problem is fixed at this point.

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