thelamb 163 Posting Pro in Training

A class that has _any_ pure virtual function cannot be instantiated directly. So not only complete Abstract classes...

class myBase
{
    virtual void pureVirtual() = 0;
    void normal() { };
};

class myClass : public myBase
{
    void pureVirtual() { }
};

int main()
{
    myBase base(); // Error, cannot be instantiated
    myClass class(); // Fine
}
thelamb 163 Posting Pro in Training

Ok first of all: Why are you passing this list as pointer and not a const&? You are not modifying the passed list in any way...
If you insist on using a pointer to pass it, we need to see how you're calling this function to be able to see what's wrong.

thelamb 163 Posting Pro in Training

Any compiler will tell you that's wrong.
When you allocate an array statically(as oppose to dynamically) the size of the array _must_ be available at compile time. In your example, maxElements is a variable so the compiler has no idea if it's going to change at runtime.

So you have 2 options:
- allocate the array dynamically
- make maxElements a 'const int'

If you don't know how, ask ;)

thelamb 163 Posting Pro in Training

Well I can imagine that, I work closely together with a VB.net programmer and in the beginning she had more trouble understanding my code than I had understanding hers.

However, now she can follow most of what I write eventhough she hasn't written a line of C in years.

So I think the answer is that C++ is generally more difficult and will take more time to grasp... but when you do you have something powerful at the tip of your fingers ;).

(One thing that C++ does a lot is leaving the programmer to decide how to approach a problem... the same goal can be achieved in many many ways which to me was also very confusing when I started).

thelamb 163 Posting Pro in Training

First of all, use code tags to paste code...

So let's look at your error, it is complaining that it cannot convert an int* to an int. And the error occurs in getValue.

getValue is declared to return an 'int', and in the function you return 'value' which is declared as int value[10];

What you should read up about are pointers and arrays. The value variable will store 10 int values.. so it may look like;

value[0] = 10;
value[1] = 3;
...
value[9] = 2;

So when you want to access _one_ of these elements, you use 'value[ i ]' where i is the index of the value you want to access. Now if you use just 'value' without any array-index it means 'pointer to the first element of the array' so of type 'int*'.

In the getValue function you do return value; so it is trying to return an int* but getValue should return int... that's an impossible implicit conversion.

Instead, your getValue should return something like value[0] or value[2] depending on which element of the array you want to return.

thelamb 163 Posting Pro in Training

What your teacher said is true to some extent, a lot of programming techniques are applicable to multiple languages. This does not mean that every language is equally easy to learn. C and C++ are lower languages than languages like VB, java etc. generally this also means that are harder to understand. Compare it to Assembly language, which is the lowest 'humanly readable' language.. and it's much harder to understand than C/C++.

Also, you say VB seems easy to you... but getting a program to work does not mean it's correct, you may have used bad or ineffecient techniques or only aimed at small problems: there are aspects of VB that are difficult to grasp as well.

So don't give up on C++ just because it seems so different/difficult at start... if you really love programming you will get through it and appreciate the power of C++ ;).

thelamb 163 Posting Pro in Training

VisualStudio has a free edition (called the Express edition).
It has some downsides though if you plan to release your programs to the public(requires microsoft c++ redistributables to be installed on the target system, but it mostly is already).

I've used VS when I started, but have since moved to Code::Blocks and I absolutely love it ;). VS is great.. but C::B just feels better for me, and I love that I can just change some behavior if I don't like it and recompile.

thelamb 163 Posting Pro in Training

Yes, so you could do:

int a = isbn[0] - 48;

You need to deal with the X of course... but that should not be too hard if you know that isbn[ i ] - 48 has to be in [0-9]

thelamb 163 Posting Pro in Training

Hmm well let's think of another way... you know that '1' is ASCII 49, so if in your original code you substract 48, you get the integral number. Since you know that the ISBN consists of 0-9 this is imo an acceptable solution.

thelamb 163 Posting Pro in Training
thelamb 163 Posting Pro in Training

It's not outputting the address of the values.. an address looks like 0x00401000 and the individual elements of the string wouldn't just take 1 byte (and also.. '88' doesn't really follow 57 :P).

Basically, you have stumbled upon something called 'implicit conversion'. When you access isbn[ i ] the datatype is 'char', and you are storing it in an int variable (the a through j variables). So what happens is the compiler is implicitly converting the character it finds at index i to an int.
Now if the character at index 0 is '1' and the compiler is converting that you should get 1 in your 'a' variable shouldn't you? Wrong! Instead you get the ASCII value of '1' in your 'a' variable (see www.asciitable.com '1' is ASCII 49).

So you need to find another way...
- You can convert a char* to an int using atoi (abbrevation for 'array-to-int'). But this is a C-style function.
- A C++ approach would be to use 'std::stringstream'.

I'll leave it to you to read up on these topics and if you're stuck.. ask again.

thelamb 163 Posting Pro in Training

Are you maybe executing a release build outside of VS, and inside a debug build? In that case there could be an assertation in the function.

The documentation does not state that the handle must be valid... it does state that if the handle is closed while the wait is not satisfied, the function behavior is undefined...

What is the return of GetLastError() after the WAIT_FAILED return? It should probably be something like INVALID_HANDLE.

In any case... why are you calling this function with an invalid handle anyway? ;)

thelamb 163 Posting Pro in Training

CNoFila is a class... and you are comparing it with operator !=.
But how can it compare your class with NULL?

This comparison only makes sense if you implement operator!= for you CNoFila(Google for 'Operator Overloading') or if 'Actual' is a pointer to CNoFila (a pointer can be NULL).

thelamb 163 Posting Pro in Training

How do you think it should be done? What have you tried yourself?
Just saying it is difficult for you gives us no indication what you're having trouble with understanding.

thelamb 163 Posting Pro in Training

The question is to assign a char, to an int variable.
Meaning you will need to declare an int:

int myInt;

And then assign a char to it:

myInt = 'c';
// or:
char myChar = 'c';
myInt = myChar;

The point of this assignment is to show you that C++ can do implicit conversions between some datatypes. Sometimes this can be handy but in a big program it can become unclear when it is performing an implicit conversion.. and it is not always clear to the reader of your code if this was your intention.
That's why in C++ explicit conversions are preferred normally. If you're interested you can look up 'static_cast', 'dynamic_cast' etc.

Edit:

Printing out a char is just as easy:

char myChar = 'c';
int myInt = myChar;

cout << "myChar: "<<myChar<<" myInt: "<<myInt<<"\n";
thelamb 163 Posting Pro in Training

As far as I can tell, in "(a14*pow(temp,(12-14))" the ( is unclosed.
It's on the 5th line in your big calculation.

It would help if you format this big calculation a little.. it doesn't matter if there are spaces or tabs around math operations... that way it would be much easier to spot the problem.

Also... you forgot the ; after the big calculation.

thelamb 163 Posting Pro in Training

I assume you are re-opening the same file because you first want to create it and then edit the existing file. There would be no problem if you don't re-open the file and just to seekp and put(c).

What you want is of course possible with fstream, it would be horrible to have to revert to C-code just for this.

Here is how you do it:

out.write((char *) &h, sizeof h );
out.close();

out.open("file2", ios::in |ios::out |ios::binary); // <-- also open with ios::in
out.seekp(5,ios::beg);
c='z';
out.put(c);
out.close();

It's a subtle difference... but if you don't include ios::in the original content is truncated when overwriting existing data (not if you are appending.. then ios::in is not needed).

thelamb 163 Posting Pro in Training

Well, allocating sort of a 'pointer pool' will be more efficient if you will be doing a lot of allocation/deallocations. But if you use a vector this can be done more easily. E.g. at startup you can say 'vInstances.resize( some_number )' and it will pre-allocate memory of this size. If the vector then grows past this size the vector will internally allocate more memory.

So, in essence.. this pre-allocated array you have exposes a lot of implementation detail (the Existance-array) that you really don't want to be dealing with. So definitely have a look at vectors, and at resize.

This 'problem' is simmilar to an assigment that we let students of the OperatingSystems at my univeristy do. They are provided with a structure, and a benchmark program which allocates and deallocated thousands of instances. They need to code a 'manager' to speed this up.
What they mostly do is write a wrapper for malloc(), the first time it is called they allocate 100 * size_of_structure, and whenever a request is made for a new allocation, they increase an index and just return a pointer somewhere in this pre-allocated pool.

Performance of 100000 times 100 allocation/deallocations on my machine was 0.11 second with the new malloc versus 0.5 without any pre-allocation.
So this is not a difference that you are likely to notice.

thelamb 163 Posting Pro in Training

Hmm, your method to search for the next 'available' pointer-'spot' is not very efficient.
You are using C-style arrays... which is no necessarily wrong but what you're trying to do is much easier by using C++ vector's. You won't need the 'instanceExists' array then:

#include <vector>
using std::vector;

vector<baseObject*> vInstances;

createInstance()
{
   baseObject* pBase = new something();
   vInstances.push_back( pBase );
}

vectors are maybe slightly slower than bare C arrays, but you need to be very confident with working with arrays to beat the vector's performance (e.g. writing correct search algorithms etc.).

What don't you understand about virtual functions? You use them (almost) correctly. One of the purposes is if you have multiple types of object, which all share a common functionality, so they inherit from one base class. But there can be some functionality that is implemented differently for the different types of objects.
Consider shapes(circle, rectangle etc.) they all have dimensions, and they all have an area... but the area of a circle is calculated differently than the are of a rectangle. So the base class defines a virtual area functions, and all shapes implement this in their own specific way.
Also, if you call virtual functions through a pointer or a reference of the base class the compiler will know that it has to call the virtual function of the derived class.

What can be improved in your example:

class baseObject
{
    public:
    virtual void Step() = 0;
};

the …

thelamb 163 Posting Pro in Training

In all the programs that we have made, all the headers(#include <iostream>...etc) are
in the top of the cpp file in the source folder. Why is there a folder that is named header files then?

Header files define prototypes of functions that are implemented either in a library (like the standard C++ library) or your own functions, in a different .cpp file.
By putting '#include <iostream>' at the top of your cpp file, you are making the functions that are defined in the iostream header available to that specific .cpp file.
iostream defines functions/classes like 'cout' and 'cin' that you have probably used. Without #include <iostream>, the cout and cin would be undefined (try to remove the include and compile your source).

The 'header' folder is there for your own header files, if you create one in VisualStudio(VS) it will automatically place it in this 'virtual' folder, to keep the project clean.

Can there be more than one cpp file in the source file? If so, why?

I don't understand this question.. the cpp IS a source file, I suppose you mean if a project can have multiple .cpp(source) files.. and the answer is yes, imagine a huuuge project being written in just 1 cpp file.

For example:
Create a 'printFunctions.h'

#ifndef PRINTFUNCTIONS_H_INCLUDED
#define PRINTFUNCTIONS_H_INCLUDED

void printInt( int myInt );

#endif

And 'printFunctions.cpp'

#include "printFunctions.h"

//Implement printInt:
void printInt( int myInt )
{
    cout << "Printing an integer: " << myInt …
thelamb 163 Posting Pro in Training

You can place the entire (nameless)struct's declaration in the class header.

e.g.:

class myWrapper
{
    struct 
    {
        int houseNumber;
    } address;

    public:
        myWrapper() { address.houseNumber = 555; } // this is valid
};

int main
{
    No way to access address from here, apart from a public interface.
}
thelamb 163 Posting Pro in Training

Ok no worries then ;), keep experimenting!

thelamb 163 Posting Pro in Training

Can I ask why you want to use dynamic memory allocation in this case? Rather than just using 'bool hasSetPages;' and forget about new/delete;

thelamb 163 Posting Pro in Training

If this is something that you commonly 'mess up', consider writing code like:
if( true == myBoolean ) instead of if( myBoolean == true ).

If in the first case you write if( true = myBoolean ) by accident, the compiler will start complaining (the same works with other conditional statements, such as while).

thelamb 163 Posting Pro in Training

So it does not say that it cannot find libboost_regex.so?
In that case: does /usr/lib contain any other boost libraries?

If I remember right.. (I've used this library a while ago) the regex lib depends on some other boost libraries that you need to link to aswell. Maybe post the errors your getting for a better understanding of the problem.

thelamb 163 Posting Pro in Training

I've had the same 'problem' as you when I started (I'm also selftaught)... but I don't really understand what error handling has to do with it... but let me comment on that first:

You created your own macro that in debug-mode prints some information about the error.. this is a fine idea, you could also use assertations (lookup ASSERT). But in many cases, you also want to have some error-reporting in a release build.

I mostly use an exception class for this (CExcept), which inherits from std::exception. I overload operator<< in this class.
I can then do error checking:

void myFunction()
{
    if( hasError() )
        throw CExcept( SOME_EXCEPTION_ENUM ) << "myFunction failed horribly";
}

int main()
{
   try {

   } catch( CExcept& except )  // or std::exception& 
   {
        if( HORRIBLE_EXCEPTION_CODE == except.getCode() )
            err_out << except << "\n"; // in Debug build, this won't do anything
   }
}

err_out either functions as cout, or some file stream object or it could even be a class which emits the message as messagebox etc...
And it implements a << for ( err_out, except ) of course.

This way, if I want to change something as to how errors are handled in general I can do it in one place (Want errors to output to file? Just change err_out).
Also... a lot of functions set some error code (GetLastError in Windows or errno in Linux) with a simple #ifdef _WIN32 or #ifdef __linux__ in the overloaded …

thelamb 163 Posting Pro in Training

There are left over characters in the buffer after the first cin.getline(), the second cin.getline() is picking this up.

Read this:
http://www.daniweb.com/forums/thread90228.html

thelamb 163 Posting Pro in Training

Any decent compiler should have the short circuit evaluation (it still blows my mind why a compiler would do otherwise).

So if you think about your own change/question ... what do you think will happen?

thelamb 163 Posting Pro in Training

Just for the fun of it, what does the OP(or someone else) think the ouput of this snippet will be:

#include <iostream>
using std::cout;

bool returnTrue ( ) { cout << "ret TRUE\n"; return true; }
bool returnFalse( ) { cout << "ret FALSE\n"; return false; }

int main()
{
    if( returnTrue() || returnFalse() )
        cout << "toot\n";
}

Note to devs:

Maybe it's an idea to add some 'SPOILER' tag? So with questions like these I could put the answer in SPOILER tags, so anyone who wants to think about it for him/herself before seeing the answer can chose so. (Ok Ok the real reason is that I won't look bad if I forget about this thread and someone continues on the question)

thelamb 163 Posting Pro in Training

Yes, that makes absolute sense.

So what do we make of the OP's problem... compiler not following the standard?

Edit:
Maybe the OP should try to compile his code in 'release' mode.. with all optimizations enabled. If it still attempts to evaluate the v[2]... I would chose a different compiler ;)

thelamb 163 Posting Pro in Training

I guess you mean "if the first evaluates to false" instead?

Oeps, fixed.

On compile time it has no idea if v[2] is going to be valid or not, so if anyone has a reason why a compiler would decide to emit code that evaluates this second expression... please share.

thelamb 163 Posting Pro in Training

If I remember the C++ programming language book correctly the standard states that the second expression should not be evaluated if the first evaluates to false(would be pointless)... but maybe the OP's compiler doesn't follow this standard strictly.. or I am remembering wrong and it's not actually in the standard.

In any case, consider this (maybe the OP should try this code):

#include <iostream>
using std::cout;

bool returnTrue ( ) { cout << "ret TRUE\n"; return true; }
bool returnFalse( ) { cout << "ret FALSE\n"; return false; }

int main()
{
    if( returnTrue() && returnFalse() )
        cout << "toot\n";

    if( returnFalse() && returnTrue() )
        cout << "toot\n";
}

OUTPUT (for me):

ret TRUE
ret FALSE
ret FALSE

So in the second case, the returnTrue is not evaluated.

I'm not really sure why, or even how the OP's compiler emits this as ASM... if I were to look at the ASM code of what I just posted, it would call returnTrue and compare it to true, if the comparison fails there would be a JMP over the entire If-block, skipping the second comparison.
It doesn't make sense to me why the compiler would decide to evaluate both and then compare both...

thelamb 163 Posting Pro in Training
void Map::LoadMap(const std::string& filename) {
	std::ifstream file;
 
	if(!file.is_open()) {

is_open() will _always_ return false, no matter what 'filename' you hand over to this LoadMap function.
Think about what you're missing, how will ifstream 'know' which file to open?

thelamb 163 Posting Pro in Training

Post the header file you're including (if it's small) and how you include it...

Could be a missing #endif, or a missing ; etc.

thelamb 163 Posting Pro in Training

The [out,retval] looks like an annotation to me, but I have never seen it this way. What compiler do you use?

If it is an annotation it is basically an aid for the compiler, or some code verification tool. For instance if you as programmer are to delete the returned pointer, a verification tool has to know what the returned pointer is... and check if you really are deleting it.

Especially microsoft is doing a lot of (good) work in this, in the field of driver development pretty much anything can be annotated.

thelamb 163 Posting Pro in Training
thelamb 163 Posting Pro in Training

Why are you so fixed on ReadProcessMemory? What's wrong with my suggestion in your case?

thelamb 163 Posting Pro in Training

Why do you need to read the game server's memory to check if it is online?
Take a look at HLSW for example(or any gameserver-listing tool) it basically queries the server on it's game port to see if it responds correctly... you could do the same and restart the gameserver if it is not responding.

Or you can simply check if the process is still running (though in that case you don't really know if the server is connectable... e.g. it could be running without a map loaded).

thelamb 163 Posting Pro in Training

Let me get this straight.. the code you are currently posting (with the temp stringstream) is working, but if you write

arq_linha_part >> parm;

It gives you an error?
If so, then it should give you an error. The short answer is that std::string (the arq_linha_part variable) does not have a >> operator that takes double (the parm) as second argument.

So one of the ways to convert an std::string to a double is by using an std::stringstream like you have in the current code.

Also.. in future it is probably not necessary to paste all of the compiler errors, after the first error the compiler usually gets totally confused and will spit out many more errors that might not be errors anymore after you fix the first few errors.

thelamb 163 Posting Pro in Training

We'll need more information than this... generally ReadProcessMemory should not crash the target application of course, so it seems you're doing something 'wrong'... without knowing what exactly you're doing we're in the dark here ;).

Try to down-size the code as much as possible while still crashing the target (e.g. just OpenProcess and ReadProcessMemory): Does it still crash? Does it crash after commenting the ReadProcessMemory call?
Then I think you can make a more informative post.

thelamb 163 Posting Pro in Training

I don't remember exactly, but some years ago I tried the same, and it was a downright pain in the ass to get it to work, so in the end I just went back to the microsoft C++ compiler... it will save you a lot of trouble.

In any case it wasn't just a matter of pointing MinGW to the DirectX include/lib directory...

thelamb 163 Posting Pro in Training

It compiles fine here... can you copy/paste exactly what it says?

thelamb 163 Posting Pro in Training

Before expecting any comments from us, please show us what you have done yourself. Did you think about it atall? If yes.. what are your thoughts? If no... think about it first.

thelamb 163 Posting Pro in Training

It is impossible to say how much faster your code wil run, if any faster atall, if you 'use vectors or pointers'. Generally using vectors will even be slower than using raw pointers.

I'm not sure how you expect to see a performance increase by using pointers... in certain areas using a pointer can speed something up, but it would probably be a better idea to look at your algorithm first (maybe even profile it to see where the bottlenecks are, if it's really that important).

Thinking about how many instructions your CPU is going to execute doesn't really have a lot of meaning, as Nathan mentioned a CPU can perform 2 billion instructions in a second but that says nothing about how many instructions your running process gets to execute, in fact it will probably not even run on the CPU consequtively for an entire second.

And as to your question regarding an instruction... the examples you mention like a file transfer could consist of thousands of instructions(and a lot of time waiting for the hardware, in which case the CPU can decide to calculate something for another process while waiting).

thelamb 163 Posting Pro in Training

http://en.wikipedia.org/wiki/Reference_(C%2B%2B)

took 2 seconds to find

thelamb 163 Posting Pro in Training

No, Banfa is right.
You are correct that the first element is located at index '0', and you know that there are in total 5 elements.

0 - first element
1 - second
2 - third
3 - fourth
4 - fifth (bingo, end of array)
(5 - sixth, non-existing)

thelamb 163 Posting Pro in Training

yay :D

Now get rid of that 'system("pause")' call... check the sticky's on this board for information about how to 'properly' pause the execution of your program.

thelamb 163 Posting Pro in Training

No... you're still doing _exactly_ what I said you should 'never ever' do...

I really don't know how I can make it more clear other than writing the code for you, which defeats the point of you learning how to do it.

Do you know that this is possible:

int xValue = 5;
int yValue = 6;

node* newNode = new node();
newNode->infoX = xValue;
newNode->infoY = yValue;

delete newNode; // Don't forget to free the memory once you are done with it!!
thelamb 163 Posting Pro in Training

" I tried the idea of creating two newNodes and storing the y data in it. "

That's not what I meant... you were already creating two newNodes in your original solution, and I tried to make you think about _why_ you do that.

Just think logically, you have one structure that has a place to store infoX and infoY _together_. So when you ask the user to input X and Y, why do you want to store this in 2 different structures instatiations?

What I just realized is that in your original solution you defined one newNode pointer, and just called newNode = new node() twice... And now you changed it to newNodeX and newNodeY, I missed that in my first post, I thought you already had a newNodeX and newNodeY.


In any case, I think you don't understand the use of the 'new' operator.

int* pInt;   // pointer to an int variable
int* pInt2;  // Idem.

// Both pInt and pInt2 are 'lose' pointers, they are totally useless at the  moment.
// So let's make them useful:
pInt  = new int;
pInt2 = new int[2];
// Now the new operator has allocated memory where pInt and pInt2 will be stored.
*pInt = 5;   // 'value-pointed-by' pInt is now set to 5
pInt2[0] = 10; 
pInt2[1] = 15; 

// Because we've dynamically created the storage for pInt and pInt2, we also need to free it:
delete pInt;
delete[] pInt2; // pInt2 is …
thelamb 163 Posting Pro in Training

The sockets you 'accept' are set to blocking, which means they can wait indefinitely until new data is ready to be recv'd. So if you are only sending 2 strings on that socket, the loop will stick there if there is no data to be read.

I'm having trouble understanding why you check mConnectionPool.size() for < 1 or > 0, and then doing almost the same things, apart from setting non-blocking in one of the cases. But after this if-else you set the socket to blocking anyway.

So in the end the socket is always going to be set to blocking-mode.