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
Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


TSDK posted:

Yes, you're suppose to delete[] it, even if the array size is 1. Could well be a compiler bug then, although I usually like to dive into the actual asm generated and check it's doing something stupid before declaring that. Double check that elsewhere in the code there's nothing assuming mRefreshBuffer has more than one element.

Well, I just finished combing through every use of that buffer with very fine teeth, and nothing out there makes that assumption. DID fix a bug where the new[] could be size 0, but that's unrelated in this case. Didn't find a single problem that could be related.

I don't get it, I put in that if statement for deleting and ran the thing with libumem debugging fully on, libumem debugging off, and the standard memory library, and it never sees a problem. Generates the same output every time.

I guess for now I'll slap a todo comment in there and write it off as fixed so we can get it integrated, and in the mean time, start combing through the asm. Run it through dbx's access checker, too, but gently caress me that turns a three-minute run time into like six hours, ugh.


Anyway, thanks for the help, all. Duly appreciated. :)

Adbot
ADBOT LOVES YOU

That Turkey Story
Mar 30, 2003

Ledneh posted:

Well, I just finished combing through every use of that buffer with very fine teeth, and nothing out there makes that assumption. DID fix a bug where the new[] could be size 0, but that's unrelated in this case. Didn't find a single problem that could be related.

I could be mistaken, but I think you are allowed to dynamically allocate 0 length arrays (though only dynamically).

L:ordSilent
Jul 28, 2004
Hey goons. C++ noob here wondering how to do classes and split it into 3 files. One with the class, one with the functions, and another with the actual program. In order to try to get used to it I tried to make a program that only multiplied one number but it seems I can't even do that. Can you guys tell me where im going wrong?

code:
//classtest.cpp
#include "stdafx.h"
#include "Header1.h"
#include "func.cpp"


int main()
{
    int num;
	test t;
    t.multiply(num);
    t.print(num);
}
code:
//Header1.h
#include "func.cpp"
class test 
{
    private:  
            int rnd;
    public:
            int multiply(int num);
            void print(int num)
};
code:
//func.cpp
#include <iostream>
using namespace std;
int multiply(int num)
{
    num = 26 * 8;
    return  num;
}
void print(int num)
{
    cout << endl << "This is the number " << num;
}
thanks for helping the noob.

L:ordSilent fucked around with this message at 02:12 on Jan 31, 2009

sklnd
Nov 26, 2007

NOT A TRACTOR

L:ordSilent posted:

Can you guys tell me where im going wrong?

code:
#include "func.cpp"
Don't do this. Including cpp files is bad juju. That both exposes your implementation and forces a rebuild of everything that includes it when the cpp changes. Also, what you really want is to include your .h instead, since that has the class definition.

code:
//func.cpp
#include <iostream>
using namespace std;
int multiply(int num)
{
    num = 26 * 8;
    return  num;
}
void print(int num)
{
    cout << endl << "This is the number " << num;
}
These should be declared as member functions for the test class, like this:
code:
int test::multiply(int num)
Also you have num as a parameter by value to multiply for no reason. It's also uninitialized when you pass it.

sklnd fucked around with this message at 01:02 on Jan 31, 2009

julyJones
Feb 12, 2007

Stopped making sense.

Plinkey posted:

It complies but when I try to use the functions from the DLL (memory allocation function RD_new() ). I get a run time error. It's a "System.DllNotFoundException". The DLL and .lib files are in the build directory and looking stuff up on google isn't helping too much. Any ideas?

I'm not familiar with the type of development you're doing, but when I used VB 6 on a past project, I had to run "regsvr32" to get Windows to register any third-party DLL's I wanted to load.
http://technet.microsoft.com/en-us/library/bb490985.aspx

L:ordSilent
Jul 28, 2004

sklnd posted:

what you really want is to include your .h instead, since that has the class definition.
Which file?

sklnd
Nov 26, 2007

NOT A TRACTOR
The one with the //Header1.h comment.

Gary the Llama
Mar 16, 2007
SHIGERU MIYAMOTO IS MY ILLEGITIMATE FATHER!!!
Quick question about references. According to Wikipedia:

quote:

In many implementations, normal parameter-passing mechanisms often imply an expensive copy operation for large parameters. References qualified with const are a useful way of passing large objects between functions that avoids this overhead...

Let's say I'm passing in a large object - a texture, for instance - why do I have to pass it in as a const reference instead of just a non-const reference? Isn't a reference just the address of the variable being passed in?

And what if I need to modify the object being passed in? If I can't pass it via const reference, should I just pass it via pointer? Am I terribly confused or slightly on the right track? :)

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Non-const reference has different semantics than value-types, which is why they are referring to const references. (i.e. If I see a function that takes a non-const reference, I will assume that it will modify my object. If I see a function that takes an rvalue reference, I will assume that it will destroy my object.) Pointers are useful for pointer-to-array types and for nullable values. You can do whatever you want, though.

Also, Wikipedia is not a reference for C++ programming, and it's honestly kinda crap for that purpose.

Smackbilly
Jan 3, 2001
What kind of a name is Pizza Organ! anyway?

L:ordSilent posted:

Hey goons. C++ noob here wondering how to do classes and split it into 3 files. One with the class, one with the functions, and another with the actual program. In order to try to get used to it I tried to make a program that only multiplied one number but it seems I can't even do that. Can you guys tell me where im going wrong?

"Don't #include cpp files" was already said, but here's a quick primer on what the distinction between h files and cpp files is:

foo.h describes the interface to foo.cpp

In other words it makes a promise that certain things will be available in foo.cpp without the compiler having to look at foo.cpp directly.

Your source code goes through two processes on its way to become an executable - compiling, and linking. When you compile a program that uses multiple source files, each source file (cpp file) gets compiled into its own "object file". foo.cpp will get compiled into foo.o or foo.obj (depending on your compiler/platform).

The key part is that when you compile foo.cpp into foo.obj, and it uses functions that you wrote in bar.cpp, the compiler doesn't need to compile bar.cpp at all. All it needs to know is what the functions/classes/etc in bar.cpp are called and what their methods, arguments, return values etc are. That's what bar.h is for. foo.cpp uses the functions in bar.cpp by including bar.h (not bar.cpp), and foo.cpp can get compiled into foo.obj without knowing or caring what the implementation in bar.cpp looks like. bar.cpp then gets compiled separately into bar.obj.

Then, in stage two, linking, another program called a linker (usually automatically invoked by your compiler) will take all the obj files and combine them all into a single executable. Its main job is this: foo.cpp used some functions/classes/whatever defined in bar.cpp. It was able to do this without including bar.cpp directly because bar.h promised that functions/classes/whatever matching those signatures would exist in a different object file linking time. The linker then finds the implementation of these functions/classes/whatever in bar.obj and creates a link between the invocation in foo.obj and the implementation in bar.obj so that the function can be called for real when the executable runs.

When I was myself a noob programmer, figuring out how this worked cleared up a lot of things that were confusing the hell out of me when I first discovered that #include'ing cpp files isn't really manageable past one or two source files.

L:ordSilent
Jul 28, 2004
Im sorry, I still don't quite get it. can someone kind of illustrate what file im supposed to include where? Im not sure how i can get the header file to use the functions in func.cpp without including it.

Standish
May 21, 2001

L:ordSilent posted:

Im sorry, I still don't quite get it. can someone kind of illustrate what file im supposed to include where? Im not sure how i can get the header file to use the functions in func.cpp without including it.
code:
// classtest.cpp
#include "func.h" // rename "Header1.h" to "func.h"; header files should have the same basename as the .cpp file they relate to
...
code:
// func.h (renamed from Header1.h)
// ... don't include any of your files
code:
 // func.cpp
#include "func.h"
...
Then just make sure your compiler is linking classtest.o and func.o together in the linker stage (compiler dependent and you haven't said which one you're using).

L:ordSilent
Jul 28, 2004
using Microsoft Visual Studio 08
Link errors now ugh

L:ordSilent fucked around with this message at 11:43 on Feb 1, 2009

Smackbilly
Jan 3, 2001
What kind of a name is Pizza Organ! anyway?

L:ordSilent posted:

using Microsoft Visual Studio 08
Link errors now ugh

What are the link errors? If they talking about things that are missing, make sure that both .cpp files are actually getting compiled. If they are talking about multiple definitions, make sure that you're not still #including some cpp file. Post the errors.

Edit: This is probably not causing your linker error, but there's one more thing about header files that you should probably know when making programs with multiple source files: The C++ compiler is dumb and will not know not to #include for a second time a .h file that has already been #included. For example, if foo.cpp includes bar.h and baz.h, but baz.h also includes bar.h, bar.h will be included twice and the compiler will get very confused. The canonical way to avoid this problem is something called "include guards". These are preprocessor commands that ensure a .h file is included at most once. They look like this, for a file called foo.h:

code:
#ifndef FOO_H
#define FOO_H

#include <string>
// etc...

class Foo {
 // ...
};

#endif
The three preprocessor commands #ifndef, #define, and #endif do this: Check if the preprocessor has the FOO_H symbol "defined". If not, define it and process the header. If so, skip the entire header. Thus, he first time the header is #included, it defines FOO_H, and if the header is ever #included a second or third time, the FOO_H symbol will already be defined, and the header will be skipped.

Multiply-including a header normally causes a compile error (not a linker error), so this is not your issue, but you will need to know this as your programs become less trivial.

Smackbilly fucked around with this message at 16:55 on Feb 1, 2009

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.

Smackbilly posted:

What are the link errors? If they talking about things that are missing, make sure that both .cpp files are actually getting compiled.
And that the functions are actually getting defined.

Smackbilly
Jan 3, 2001
What kind of a name is Pizza Organ! anyway?

Mustach posted:

And that the functions are actually getting defined.

Right, and that the definitions match the declarations. If foo.h says:

code:
class Foo {
 public:
   void doCrap();
};
And then foo.cpp needs to say:

code:
void Foo::doCrap() 
{
 // ...
}
and not

code:
void doCrap() // Note the missing Foo::
{
  // ...
}
or

code:
bool Foo::doCrap() // Note the differing return type
{
  // ...
}
Although the latter issue (mismatched return type or argument type) is usually a compile error for class methods, it will be a linker error for free functions.

Vandorin
Mar 1, 2007

by Fistgrrl
For some reason, in my code whenever I enter a number, it only says what the cost of number four is. Here is my code so far, and I cannot figure out what is wrong with my " if else" statements.

code:
#include <iostream>
#include <iomanip>
using namespace std;

void main()
{
	float DVDnumb;
	float four;
	float DVDfour;
	float D4;
	float three;
	float DVDthree;
	float D3;
	float two;
	float DVDtwo;
	float D2;
	float one;
	float DVDone;
	float D1;


	D4 = 19.99 * .07;
	DVDfour = 19.99 + D4;
	four = DVDfour;

	D3 = 16.99 * .07;
	DVDthree = 16.99 + D3;
	three = DVDthree;

	D2 = 13.99 * .07;
	DVDtwo = 13.99 + D2;
	two = DVDtwo;

	D1 = 8.99 * .07;
	DVDone = 8.99 + D1;
	one = DVDone;


	cout << fixed << setprecision (2);
	cout << "Welcome to Netflix movie Rental! Please choose how many DVDs you would like to rent each month." << endl;
	cout << "Netflix movie rental has several different options to choose from : " << endl;
	cout << setw(10) << "4 DVD at-a-time" << setw(3) << "      " << setw(10) << "Unlimited!" << setw(2) << "$19.99" << endl;
	cout << setw(10) << "3 DVD at-a-time" << setw(3) << "      " << setw(10) << "Unlimited!" << setw(2) << "$16.99" << endl;
	cout << setw(10) << "2 DVD at-a-time" << setw(3) << "      " << setw(10) << "Unlimited!" << setw(2) << "$13.99" << endl;
	cout << setw(10) << "1 DVD at-a-time" << setw(3) << "      " << setw(10) << "Unlimited!" << setw(2) << "$8.99" << endl;
	cout << "*plus any applicable tax" << endl;

	cout << "Please type the number of DVDs you would like to rent" << endl;
	cin >> DVDnumb;
	
	
	
	if ( four = DVDnumb ) {
		cout << "Thank you! Your total cost will be:" << "  $" << DVDfour<< endl;
	} else if ( three = DVDnumb ) {
		cout << "Thank you! Your total cost will be:" << "  $" << DVDthree << endl;
	} else if ( two = DVDnumb ) {
		cout << "Thank you! Your total cost will be:" << "  $" << DVDtwo << endl;
	} else if ( one = DVDnumb ) {
		cout << "Thank you! Your total cost will be:" << "  $" << DVDone << endl;
	}

	system("pause");
}
Does anyone know why my code is only showing the cost for number four? I thought that because I used the "else if" statement, that if the number inputed "DVDnumb" was not equal to four, it would skip the statement until it got until one it was equal to.

Vandorin fucked around with this message at 01:54 on Feb 2, 2009

Avenging Dentist
Oct 1, 2005

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

Vandorin posted:

Does anyone know why my code is only showing the cost for number four? I thought that because I used the "else if" statement, that if the number inputed "DVDnumb" was not equal to four, it would skip the statement until it got until one it was equal to.

Turn on "-Wall" in your compiler options and check the warnings you get back. :)

sklnd
Nov 26, 2007

NOT A TRACTOR
code:
if ( four = DVDnumb ) {
That assigns DVDnumb to four, and unless DVDnumb is zero that will evaluate to true.

you probably want ==

Vandorin
Mar 1, 2007

by Fistgrrl

sklnd posted:

code:
if ( four = DVDnumb ) {
That assigns DVDnumb to four, and unless DVDnumb is zero that will evaluate to true.

you probably want ==

When I put the ==, and execute it, all that happens when I type the number in is that it says, "Press any key to continue" without posting any of the data.

Avenging Dentist
Oct 1, 2005

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

Vandorin posted:

When I put the ==, and execute it, all that happens when I type the number in is that it says, "Press any key to continue" without posting any of the data.

Look at how "four" is defined. Also consider why you made a variable for this in the first place.

sklnd
Nov 26, 2007

NOT A TRACTOR
While we're at it, main has a return type. It is int, not void.

Vandorin
Mar 1, 2007

by Fistgrrl

Avenging Dentist posted:

Look at how "four" is defined. Also consider why you made a variable for this in the first place.

"four" is a float number, so that it can either be a large number or small, and I made it = DVDfour, so that the person typing would only have to enter "four" instead of DVDfour, since they might not know how to do that.

Avenging Dentist
Oct 1, 2005

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

Vandorin posted:

"four" is a float number, so that it can either be a large number or small, and I made it = DVDfour, so that the person typing would only have to enter "four" instead of DVDfour, since they might not know how to do that.

You really, really need to think about what the input from cin is going to give you.

Vandorin
Mar 1, 2007

by Fistgrrl

sklnd posted:

While we're at it, main has a return type. It is int, not void.

My teacher has only told us about void, because he said he hates typing, and that you have to add something at the end, in addition to the closing }.

sklnd
Nov 26, 2007

NOT A TRACTOR

Vandorin posted:

My teacher has only told us about void, because he said he hates typing, and that you have to add something at the end, in addition to the closing }.

Get a better teacher. void main is not valid c++, and gcc will not even compile with it.

http://www.research.att.com/~bs/bs_faq2.html#void-main

Also, the something you have to add (return) is not required in main() in c++

Vandorin
Mar 1, 2007

by Fistgrrl

Avenging Dentist posted:

You really, really need to think about what the input from cin is going to give you.

edit: I thought it would give me "four" or whatever number was entered, and then the statement " if (DVDnumb == four )" statement would start working?

Vandorin fucked around with this message at 02:24 on Feb 2, 2009

blorpy
Jan 5, 2005

You have provided us with some great laughs, A+.

Uh, just to be sure, you're not a CS major, right?

sklnd
Nov 26, 2007

NOT A TRACTOR

Vandorin posted:

edit: I thought it would give me "four" or whatever number was entered, and then the statement " if (DVDnumb == four )" statement would start working?

I think you don't understand how variables and literals work yet. You want the literal 4 instead of the variable four, and DVDnumb should be an int instead of a float. Your input should be an integral number (meaning it doesn't have a decimal point).

Avenging Dentist
Oct 1, 2005

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

Vandorin posted:

edit: I thought it would give me "four" or whatever number was entered, and then the statement " if (DVDnumb == four )" statement would start working?

"four" is a string. This is not a string.

Think about it this way: What do you want the user to do? (Input some information) What kind of information do you want? (A number, specifically an integer from 1 to 4). What does each possible input correspond to? (A choice from your list of DVDs) How can you tell if the user entered the number 1? (An if statement)

And so on

Vandorin
Mar 1, 2007

by Fistgrrl

Avenging Dentist posted:

"four" is a string. This is not a string.

Think about it this way: What do you want the user to do? (Input some information) What kind of information do you want? (A number, specifically an integer from 1 to 4). What does each possible input correspond to? (A choice from your list of DVDs) How can you tell if the user entered the number 1? (An if statement)

And so on

OH, ok I understand what you mean now, and my code is working great! :D, Thank you for helping me each step, haha even though I know I really need to work at this stuff.

notMordecai
Mar 4, 2007

Gay Boy Suicide Pact?
Sucking Dick For Satan??

I have a question no so much about syntax but more of an IDEA on how to do this loving program.

I have to do a program using inheritance as the main structure. The idea is to use inheritance to solve a way to find the area of 2D shapes and the surface area/volume of 3D shapes:

The parent/child structure should be as follows:



I was going to define a macro for every shape's length/width/height (there is no user input) but really have no idea on what to actually REUSE for this assignment. I have two .h files (2D and 3D) with every shape's class defined, but my main remains blank because I have no idea on how to approach this question.

Again, not asking for help on syntax or on how to do my homework, just an idea to work with.

EDIT: Would this be a valid way?

code:
class Shape {
    public:
      int x, y;
}
and then I can just inherit those two coordinates for the 2D shape member functions and for the 3D member functions, just define a constant z?

I may be thinking too hard about this. Any help would be great.

notMordecai fucked around with this message at 04:10 on Feb 2, 2009

sklnd
Nov 26, 2007

NOT A TRACTOR
Area and Volume virtual functions would be a start.

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.

Vandorin posted:

My teacher has only told us about void, because he said he hates typing, and that you have to add something at the end, in addition to the closing }.
Your teacher is a piece of poo poo if he gets hung up on something like that.

Avenging Dentist
Oct 1, 2005

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

Mustach posted:

Your teacher is a piece of poo poo if he gets hung up on something like that.

Especially since

ISO/IEC 14882 3.6.1 #5 posted:

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing
code:
return 0;

So your teacher is so lazy that he's actually typing more to do the same thing.

raminasi
Jan 25, 2005

a last drink with no ice
What's more conventional when using classes out of the STL: "using namespace std;" in each .cpp file in which it matters, individually importing the symbol for each class you're using ("using std::vector", "using std::list" or whatever), or explicitly qualifying every occurrence of the symbol ("for (std::vector<int>::iterator p...")? Is there even a convention? I feel weird pulling in entire namespaces to save myself typing.

Scaevolus
Apr 16, 2007

Avenging Dentist posted:

Especially since


So your teacher is so lazy that he's actually typing more to do the same thing.

He probably got confused by the warnings compilers emit when there's a non-void function without a return statement.

Avenging Dentist
Oct 1, 2005

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

Scaevolus posted:

He probably got confused by the warnings compilers emit when there's a non-void function without a return statement.

Huh?

code:
dentist@Teeth:~$ cat test.cpp 
int main()
{}
dentist@Teeth:~$ g++ -Wall -pedantic test.cpp 
dentist@Teeth:~$ 
EDIT: ditto with MSVC8 with warning level 4 (the highest).

Avenging Dentist fucked around with this message at 07:01 on Feb 2, 2009

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

Avenging Dentist posted:

Huh?

code:
dentist@Teeth:~$ cat test.cpp 
int main()
{}
dentist@Teeth:~$ g++ -Wall -pedantic test.cpp 
dentist@Teeth:~$ 

Functions of a return type other than void, which are not main(), that lack an explicit return statement, will generate a warning upon compilation. Scaevolus is speculating the the prof, being obviously fairly bad at C, saw some of these warnings and didn't know that main() was a special case.

Adbot
ADBOT LOVES YOU

The Red Baron
Jan 10, 2005

The Red Baron posted:

I posted about my Modern C++ Design-inspired Boost-ified factory patterns some months ago, but I've refined a lot of the code since then and added several new features. I'd really like to hear C&C from some of our resident C++/Boost wizards on what they think of the design/code/documentation etc, and whether or not it seems ready to be submitted to the Boost vault/sandbox any time soon.

Anyone? I'm conveniently glancing at TTS and AD :kiddo:

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