|
I'm again having trouble using private data members from a class's header file in the same class's .cpp file. I was supplied a completed stats.h and a test cpp file, and need to write stats.cpp. The problem is that I get all sorts of undeclared identifier errors when I use either my_data or my_size (from stats.h) in stats.cpp. I tried including stats.h and stats.cpp separately and together in the test file, which doesn't help (I just sometimes get double the number of errors). I made sure there's a closing }; this time. What else could I be missing?
|
# ? Jun 13, 2013 17:50 |
|
|
# ? Jun 1, 2024 20:47 |
|
hooah posted:I'm again having trouble using private data members from a class's header file in the same class's .cpp file. I was supplied a completed stats.h and a test cpp file, and need to write stats.cpp. The problem is that I get all sorts of undeclared identifier errors when I use either my_data or my_size (from stats.h) in stats.cpp. I tried including stats.h and stats.cpp separately and together in the test file, which doesn't help (I just sometimes get double the number of errors). I made sure there's a closing }; this time. What else could I be missing? Post your actual code and the actual error message. What you've told us doesn't really help. My wild guess is it's probably a static datamember and an undefine reference link error, in which case you need to define your variables.
|
# ? Jun 13, 2013 17:59 |
|
hooah posted:I'm again having trouble using private data members from a class's header file in the same class's .cpp file. I was supplied a completed stats.h and a test cpp file, and need to write stats.cpp. The problem is that I get all sorts of undeclared identifier errors when I use either my_data or my_size (from stats.h) in stats.cpp. I tried including stats.h and stats.cpp separately and together in the test file, which doesn't help (I just sometimes get double the number of errors). I made sure there's a closing }; this time. What else could I be missing? It's hard to say anything without code, but one thing that sticks out is that you typically don't #include a cpp file. You should be compiling them both separately and linking them. Can you post any code?
|
# ? Jun 13, 2013 18:03 |
|
stats.h stats.cpp test program errors
|
# ? Jun 13, 2013 18:07 |
|
You need to include stats:: before all of the function names, not just the constructors (i.e. void stats::sort( )) in the cpp file.
|
# ? Jun 13, 2013 18:12 |
|
Jabor posted:If your special-snowflake license terms forbid someone from distributing the proprietary library alongside your application without open-sourcing it, there's no way for anyone else to redistribute your application in a usable way without violating the license. Well maybe our license is further away from the LGPL than I thought, because I don't see any language like that in there. I'm not a professional license reader guy though.
|
# ? Jun 13, 2013 18:25 |
|
bloodandiron posted:You need to include stats:: before all of the function names, not just the constructors (i.e. void stats::sort( )) in the cpp file. that's what I get for copying stuff from the header file. I swear I'll get the hang of class definitions one of these days... hooah fucked around with this message at 18:56 on Jun 13, 2013 |
# ? Jun 13, 2013 18:46 |
|
Sorry for the double post, but I seem to be losing my mind. Why would this code only print out a 0?code:
code:
|
# ? Jun 14, 2013 01:29 |
|
A semicolon.
|
# ? Jun 14, 2013 01:40 |
|
hooah posted:Sorry for the double post, but I seem to be losing my mind. Why would this code only print out a 0? ^ Also, you should compile with warnings.
|
# ? Jun 14, 2013 01:40 |
|
Also, note that you want to use .size(), not .capacity().
|
# ? Jun 14, 2013 03:46 |
|
It's typically frowned upon to declare multiple local variables on a single line (while initializing some of them):code:
That doesn't make a difference in the sample you posted, because you initialize j later on before using it. But in general, it's a good idea to declare your variables as close as possible to where you use them, and to initialize them at the same time that you declare them. Since j is only used in the body of the for loop, you can declare it like so: code:
|
# ? Jun 14, 2013 04:10 |
|
I've got a somewhat problem with dynamic libraries on Linux and I'm hoping someone can help me. I've an application that directly links a static library and my application additionally links to a .so that also links to the static library. The static library has both externed and static global variables. One of the externed global variables is used as a flag to indicate whether one of the static global variables has been initialized. On Windows each copy of the static library has its own copy of both variables, but on Linux apparently the externed variables get shared between library copies but the static variables do not. This causes issues when both my application and the .so use the static variable that the externed variable is protecting because both will think they have initialized the static variable when in reality only one copy has (causing a SEGFAULT). The best way that I can think of to fix this is to make the externed guard variable static and then create an accessor function to it (it is only being set in one file, but it is being read from lots of them). Is there any better way to do this? Particularly, is there any way to make Linux behave like Windows where each copy of the static lib also gets its own copies of its externed global variables? This is some 20 year old legacy code that at least a hundred global variables so there are almost certainly more cases of this kind of problem and I'd really like to solve them all at once rather than wait until we get a bug report for each one (which will probably take the form of "it crashes randomly and we don't know why, fix it" ).
|
# ? Jun 14, 2013 21:55 |
|
wasabimilkshake posted:
Erm, no? Declaring your variables should generally be done well in advance, so that way they have memory assigned to them already. If anything, your variables *should* be in a struct (or object) for code reusability. Why would you declare j over and over and over in different functions if you could just use a single variable in a object or struct that was set at initialization. Also the code you posted only works with C99 and above, and is dependent on the compiler, it is always safer to declare your variable first, and then use it later on.
|
# ? Jun 14, 2013 22:11 |
ratbert90 posted:Erm, no? Declaring your variables should generally be done well in advance, so that way they have memory assigned to them already. If anything, your variables *should* be in a struct (or object) for code reusability. Why would you declare j over and over and over in different functions if you could just use a single variable in a object or struct that was set at initialization. That's really terrible advice. Your C++ compiler is confiscated. Please leave all your code at the reception for safe incineration so it will not inflict further damage on the world at large.
|
|
# ? Jun 14, 2013 22:27 |
|
ratbert90 posted:Erm, no? Declaring your variables should generally be done well in advance, so that way they have memory assigned to them already. If anything, your variables *should* be in a struct (or object) for code reusability. Why would you declare j over and over and over in different functions if you could just use a single variable in a object or struct that was set at initialization. This is not C code. Also, ugh, no. Neither memory nor code reusability is relevant here. Defining loop indexers "way before" the loop is only done in C89 because of the language restrictions. Not a concern in C++.
|
# ? Jun 14, 2013 22:28 |
|
And it was only a concern in C89 because they couldn't decide which scope it would be assigned to.
|
# ? Jun 14, 2013 22:42 |
|
Mr.Radar posted:I've got a somewhat problem with dynamic libraries on Linux and I'm hoping someone can help me. I've an application that directly links a static library and my application additionally links to a .so that also links to the static library. The static library has both externed and static global variables. One of the externed global variables is used as a flag to indicate whether one of the static global variables has been initialized. the ld manual posted:
ELF is weird because it was designed to be an entirely transparent drop-in replacement for standard Unix static linking behavior.
|
# ? Jun 14, 2013 22:47 |
|
ratbert90 posted:Erm, no? Declaring your variables should generally be done well in advance, so that way they have memory assigned to them already. If anything, your variables *should* be in a struct (or object) for code reusability. Why would you declare j over and over and over in different functions if you could just use a single variable in a object or struct that was set at initialization.
|
# ? Jun 14, 2013 22:51 |
|
ratbert90 posted:Erm, no? Allocating memory in advance is, as you suggest, a good idea. But local variables [that are primitives or don't have constructors that allocate more things] are always allocated in advance - it doesn't matter if they go in and out of scope, there's no reallocation there, because local variables are just spaces on the stack, which is a big pre-allocated memory lump. That's not enough to make your way a bad idea, that just takes away your reason for saying it's a good idea. The reason it's a bad idea is that there's a lot more scope for hard to find errors if you can reuse a variable somewhere outside its intended scope. Declaring variables in the smallest scope that covers their intended use makes accidental reuse of something like that that's been modified into a compile-time error instead of a "what the gently caress is going on here" error. Plus, int banana and int orange whose scopes don't overlap can occupy the same piece of the stack instead of either taking twice as much or confusingly reusing the variable name. roomforthetuna fucked around with this message at 23:14 on Jun 14, 2013 |
# ? Jun 14, 2013 23:10 |
|
ratbert90 posted:Erm, no? Declaring your variables should generally be done well in advance, so that way they have memory assigned to them already. If anything, your variables *should* be in a struct (or object) for code reusability. Why would you declare j over and over and over in different functions if you could just use a single variable in a object or struct that was set at initialization. ints are allocated on the stack anyway, int j = 0; is going to be (at worst) "push 0" where as malloc is much more time consuming :p 1999 was 14 years ago.
|
# ? Jun 14, 2013 23:12 |
|
ratbert90 posted:Erm, no? Declaring your variables should generally be done well in advance, so that way they have memory assigned to them already. If anything, your variables *should* be in a struct (or object) for code reusability. Why would you declare j over and over and over in different functions if you could just use a single variable in a object or struct that was set at initialization. Well, one particularly good reason would be the existence of a data structure where all local variables could be represented as offsets from a relative address which moves backwards and forwards with each function call and return. Such a structure would be profoundly useful. There would be no need for any memory allocations at all. The compiler could simply emit offsets tailored for each function, regardless of their declared location. Why, it could even reuse slots when others are no longer needed! Constant-time operations, superb cache locality and easy to implement to boot. I call it... the stack!
|
# ? Jun 14, 2013 23:12 |
|
roomforthetuna posted:Everyone already told you you're wrong, but I thought I'd explain why, since nobody else has mentioned it. You probably meant 'POD type' rather than 'primitive', and operations to grow and shrink the stack are not free (the arithmetic on the stack pointer you see at the beginning and end of functions takes cycles, albeit few); ratbert is wrong because the compiler isn't growing and shrinking the stack over and over in the loop (and because his idea of storing "utility variables" in one global struct is zany). Blotto Skorzany fucked around with this message at 23:19 on Jun 14, 2013 |
# ? Jun 14, 2013 23:16 |
|
roomforthetuna posted:Plus, int banana and int orange whose scopes don't overlap can occupy the same piece of the stack instead of either taking twice as much or confusingly reusing the variable name.
|
# ? Jun 15, 2013 00:01 |
|
Would whoever decided the Oracle C++ Call Interface would not support asynchronous operations, despite the C interface supporting them just fine, please step forward so I can murder them? Thanks.
|
# ? Jun 15, 2013 00:02 |
|
roomforthetuna posted:Everyone already told you you're wrong, but I thought I'd explain why, since nobody else has mentioned it. The reason is "leave worrying about low-level stuff like that to the compiler, worry about semantics".
|
# ? Jun 15, 2013 00:03 |
|
pseudorandom name posted:ELF is weird because it was designed to be an entirely transparent drop-in replacement for standard Unix static linking behavior. Thanks. I looked over the man page for ld so many times I have no clue how missed that. I'll give it a try and see if it works.
|
# ? Jun 15, 2013 00:15 |
|
Otto Skorzeny posted:You probably meant 'POD type' rather than 'primitive', and operations to grow and shrink the stack are not free (the arithmetic on the stack pointer you see at the beginning and end of functions takes cycles, albeit few); ratbert is wrong because the compiler isn't growing and shrinking the stack over and over in the loop (and because his idea of storing "utility variables" in one global struct is zany). And yes, using a global for such things is especially nuts, I pretty much just glossed over that because it was so insane. My reasons for not using variables with wider scope still apply to that though, just even more so. Also, "grow and shrink the stack" is kind of misleading to someone who is mistakenly worrying about slow memory allocation - what you do at the start of the function isn't really growing the stack, it's just moving a pointer up the pre-allocated space. The contents of the stack have grown, but the stack itself is the same lump of memory it always was. Not that a 'slow' malloc isn't the same kind of thing of course, but malloc has to be concerned about reusing spaces that have been freed, where adjusting the stack pointer has no such concerns. It's not a memory allocation.
|
# ? Jun 15, 2013 01:35 |
|
roomforthetuna posted:Also, "grow and shrink the stack" is kind of misleading to someone who is mistakenly worrying about slow memory allocation - what you do at the start of the function isn't really growing the stack,
|
# ? Jun 15, 2013 02:42 |
|
Ciaphas posted:Would whoever decided the Oracle C++ Call Interface would not support asynchronous operations, despite the C interface supporting them just fine, please step forward so I can murder them? Thanks. OCCI is a terrible, terrible thing and no one should use it ever. So is OCI for that matter, but at least you can build your own nice-ifying wrapper around it. Just not OCCI.
|
# ? Jun 16, 2013 23:08 |
|
I've heard good things about IntelliSense. Why can't it seem to complete its own sentences?quote:IntelliSense: more than one operator "+" matches these operands: This was a real error I got. How does this deserve praise?
|
# ? Jun 17, 2013 07:25 |
|
C++ IntelliSense... is not very good.
|
# ? Jun 17, 2013 13:18 |
|
Presumably ctrl+F7 would have provided the rest. I usually disable Intellisense errors in the error list because they are generally a minute behind or irrelevant. It also annoyingly spams spurious errors from files you have open which are disabled on the current platform/config.
|
# ? Jun 17, 2013 18:18 |
|
Yeah, people jerking off over intellisense are talking about c#.
|
# ? Jun 17, 2013 18:33 |
|
As a person who programs that doesn't know how to program I can confirm that intellisense will pretty much write your program for you in c#. If it suggested lottery numbers I would not be talking to you poor scrubs right now.
|
# ? Jun 18, 2013 03:21 |
|
Visual Assist X supposedly makes C++ IntelliSense much better.Rottbott posted:I usually disable Intellisense errors in the error list because they are generally a minute behind or irrelevant. It also annoyingly spams spurious errors from files you have open which are disabled on the current platform/config. Still more problematic are IS errors that don't really exist in the code.
|
# ? Jun 18, 2013 10:39 |
|
I would much rather use Visual Assist than IntelliSense. It's the lesser of the two evils. IntelliSense is a bit better in VS2012, but it's still nothing special.
|
# ? Jun 18, 2013 16:58 |
|
Visual assist X leaked memory like a sieve when I tried it in vs2010 but that was a few years ago and nobody else had that experience anyway, apparently.
|
# ? Jun 18, 2013 18:48 |
|
When I'm stepping through c++ code in vs2012, is there a way to get it to step through ONLY code I've written, or within a certain file? Just My Code option still steps through an library I'm using:(
|
# ? Jun 18, 2013 19:57 |
|
|
# ? Jun 1, 2024 20:47 |
|
I actually quite love using Visual Assist X with VS2010 at work. The two most useful things I've found, aren't directly tied to code completion, but code navigation. Alt+G on a variable goes to the definition, ctrl+shift+O opens a window that you can open files in your project by typing part of the file name. Alt+O toggles between the header and the code. There might be other addons for Visual Studio that already covers this, and in fact I'm sure Visual Assist does even more, but I've found it most useful simply for navigating a large code base quickly.
|
# ? Jun 19, 2013 06:43 |