|
No, all the header files have #include guards, which is part of my confusion as I thought they were suppose to stop this kind of thing from happening.
|
|
|
|
|
Rizean wrote: Employee and it's three child classes all include "employee_template.h"
They just include the same header file or they all derive from a class in employee_template.h?
What does the code look like in employee.h and employee_template.h?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
employee_template.h
#ifndef EMPLOYEE_TEMPLATE
#define EMPLOYEE_TEMPLATE
#include <iostream>
#include <string>
#include "stream_flush.h"
template <class T>
void _getInput(const std::string& message, T& result)
{
std::cout << message;
std::cin >> result;
flush_istream(std::cin);
if (std::cin.fail())
{
std::cin.clear();
std::cout << "Invalid input!\n";
_getInput<t>(message, result);
}
}
#endif
</t>
employee.h Just defines the base class and does not include employee_template.h however employee.ccp does.
employee.ccp
#include "employee.h"
#include <string>
#include "employee_template.h"
using namespace std;
Employee::Employee()
{
_getInput<string>("Please enter the employees first name: ", mFName);
}
}
researcher.h
#ifndef RESEARCHER_H
#define RESEARCHER_H
#include "employee.h"
#include <string>
#include <fstream>
class Researcher: public Employee
{
public:
Researcher();
Researcher();
};
#endif
researcher.cpp
#include "researcher.h"
#include <string>
#include <fstream>
#include "employee_template.h"
using namespace std;
Researcher::Researcher():Employee
{
_getInput<string>("Enter the researcgers school: ", mSchool);
}
};
I hope this helps and if you need more let me know. BTW, is there a way to cut and paste on Codeproject?
Edit: Errors from learning to post on CP...
|
|
|
|
|
What and where is "flush_istream" (since that's the one causing the error)?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
One of my instructors finally got back with me and explained to me what I was doing wrong. "flush_istream" was a function I created to deal with invalid user inputs to cin calls. The function is only five lines long and it is used by a number of other functions. I placed it in its own header files stream_flush.h, but because it was the only function and because it was so short I did not separate the definition and implementation, i.e. no stream_flush.cpp file. However, as you have probably already figured out this caused the error of it being defined multiable times. My instructor suggested I inline the function which did fix the problem, however I'm thinking it would be better to separate the definition and the implementation.
#ifndef STREAM_FLUSH_H
#define STREAM_FLUSH_H
#include <istream>
inline void flush_istream(std::istream& in)
{
while(in.peak() != '\n')
in.get();
}
#endif
Thanks all for your help. Being in Korea often leaves me sitting here for up to a day or more sometimes waiting for help. Just one of the challenges of being in the Air Force and going to school online.
|
|
|
|
|
I'm writing a class named PlayerDataVector that contains an array of pointers to PlayerData objects (player_data_array is the pointer to the array), but theres a problem when i need to create a new larger array:
Why does the line "delete [] player_data_array;" give a Debug Error "HEAP CORRUPTION DETECTED: AFTER NORMAL BLOCK (#70) at ..."?
void PlayerDataVector::CreateAndCopyToNewArray(){
int current_array_size2 = current_array_size + 4;
PlayerData **new_player_data_array = new PlayerData*[current_array_size2];
int i;
for (i = 0; i < current_array_size; i++){
new_player_data_array[i] = player_data_array[i];
}
//PointToNULL(player_data_array, 0, current_array_size);
delete [] player_data_array;
player_data_array = new_player_data_array;
FillWithNULL(player_data_array, current_array_size, current_array_size2);
current_array_size = current_array_size2;
}
|
|
|
|
|
Are you writing past the bounds of the array somewhere else in your code?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
No. player_data_array is the only pointer to the array and after the CreateAndCopyToNewArray() function has been used it points to the new array.
If I remove "delete [] player_data_array;" everything works but of course there will be an array with no pointer to it.
|
|
|
|
|
Is player_data_array initialized to NULL before the first time
CreateAndCopyToNewArray() is called?
current_array_size should be initialized too.
Otherwise, I don't see anything there that would corrupt the heap.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
modified on Sunday, June 29, 2008 4:24 PM
|
|
|
|
|
From the constructor:
current_array_size = USERID_MAX_SIZE_INIT + 1;//16;
player_data_array = new PlayerData*[current_array_size];
|
|
|
|
|
hmm beats me. Can you post a short sample that fails?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I have tried to recreate the problem in a little program that uses PlayerDataVector. But when I do that it has no problems with PlayerDataVector (the problem is when I use it in a Metamod:source plugin).
|
|
|
|
|
Someone somewhere is trashing the heap
Looks like a debugging project!
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
If I was a betting man I'd say you are. The heap doesn't corrupt itself!
Steve
|
|
|
|
|
I am experimenting with detours from Microsoft (http://research.microsoft.com/sn/detours/). I compile this to a DLL and inject the DLL into NOTEPAD.exe. I've tried both a static and dynamic injection. I am attempting to hook the MessageBox API. When loading up notepad when injected, the first MessageBox routine (before being hooked) gives an error (The application failed to initialize properly (0xc0000005)).
To test the that actual detours stuff is working, I made a console application instead of a DLL and ran the exact same code & the message box hook works.
As another sanity check, I completely removed the MessageBox hook and simple tried to call the MessageBox API from withing the injected DLL. I still get the same error.
Does anyone know when I can't call the API from a DLL but from a console application it works fine?
Below is my code:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <detours.h>
#include <string.h>
static int (WINAPI* winMessageBox)(
HWND hWnd,
LPCTSTR lpText,
LPCTSTR lpCaption,
UINT uType) = MessageBox;
int WINAPI __stdcall hookedMessageBox(
HWND hWnd,
LPCTSTR lpText,
LPCTSTR lpCaption,
UINT uType) {
return winMessageBox(hWnd, "This is a hooked message box!", "My Caption", uType);
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
MessageBox(NULL, "About to hook it", "HOOK STATUS", MB_OK);
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourAttach(&(PVOID&)winMessageBox, hookedMessageBox);
DetourUpdateThread(GetCurrentThread());
DetourTransactionCommit();
MessageBox(NULL, "Function hooked!", "HOOK STATUS", MB_OK);
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)winMessageBox, hookedMessageBox);
DetourTransactionCommit();
MessageBox(NULL, "Clearing hook!", "HOOK STATUS",MB_OK);
break;
}
return TRUE;
}
</string.h></detours.h></stdio.h></windows.h>
|
|
|
|
|
I also have the same problem, same context, almost the same app actually, any ideas why this cant be done?
|
|
|
|
|
Turns out that it wasn't my code. It was an issue with the Detours injection utility. I used a freeware DLL injector from the web, and it worked just fine.
|
|
|
|
|
My srever software:-
#include<windows.h>
#include<iostream>
#include<winsock2.h>
#include<conio.h>
int main()
{
WSAData wsadata;
if (WSAStartup(MAKEWORD(2,0),&wsadata)!=0)
{std::cout<<"winsock startup failed\n"<<WSAGetLastError();
WSACleanup();
return -1;
}
std::cout<<"winsock startup is succes\n";
SOCKET servsock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(servsock ==INVALID_SOCKET)
{std::cout<<"socket init failed\n";
WSACleanup();
return -1;
}
std::cout<<"socket init\n";
sockaddr_in sin;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_family=AF_INET;
if (bind(servsock,(sockaddr*)&sin,sizeof(sin))==SOCKET_ERROR)
{std::cout<<"FAILED TO BIND"<<WSAGetLastError();
WSACleanup();
return -1;
}
std::cout<<"Bind successful!\n";
//WSACleanup();
//while (listen(servsock,1)==SOCKET_ERROR);
SOCKET b=listen(servsock,1);
if (b==SOCKET_ERROR)
{
std::cout<<"error"<<WSAGetLastError();
getch();
return 0;
}
else
//int client;
{int len = sizeof(sin);
SOCKET client=accept( servsock,(sockaddr*)&sin,&len);
if (client==SOCKET_ERROR)
{
std::cout<<"error"<<WSAGetLastError();
}
//SOCKET client;
// while(client=accept( servsock,(sockaddr*)&sin,&len)==SOCKET_ERROR)
std::cout<<"Connection established!\n";
closesocket(client);
closesocket(servsock);
WSACleanup();
getch();
return 0;
}
}
One of my IRC friend said that my server software is terminating the accept() function quickly so my client software was giving socket init failed inspite of the fact that server software was giving "connection established ".What i am interpreting from this is that the accept() function of the server software is not returning SOCKET_ERROR
otherwise it would not have given "connection established " because my accept function is in "if" statement.But if i keep my Accept function within the while statement (lines 48 and 49)my server software gives bind successful and stops and seems as if goes into an infinite loop or something
.
My client softeware :-
#include<windows.h>
#include<iostream>
#include<winsock2.h>
#include<conio.h>
//#include<stdio.h>
int main()
{
WSAData wsadata;
if(WSAStartup(MAKEWORD(2,0),&wsadata)!=0)
{
std::cout<<"1:(startup failed\n"<<WSAGetLastError();
WSACleanup();
return -1;
}
else
{
std::cout<<"2 Socket Init Success\n";
}
SOCKET mysock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(mysock==INVALID_SOCKET )
{
std::cout<<"3:(Socket Init Failed\n";
WSACleanup();
return -1;
}
else
{
std::cout<<"4 Socket Init Success\n";
sockaddr_in sin;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=inet_addr("127.0.0.1");
sin.sin_family=AF_INET;
// if (connect(mysock,(sockaddr*)&sin,sizeof(sin))==SOCKET_ERROR)
//{std::cout<<"5:(Socket Init FAiled\n"<<WSAGetLastError();
// getch();
//WSACleanup();
// getch();
//return -1;
//}
int a=connect(mysock,(sockaddr*)&sin,sizeof(sin));
if(a=SOCKET_ERROR)
{std::cout<<"5 Socket Init FAiled\n"<<WSAGetLastError();
//getch();
WSACleanup();
//return -1;
getch();
//return 0;
}
else
std::cout<<"connection sucessful";
closesocket(mysock);
getch();
return 0;
}
}
<b>My client software is giving an output of "Socket Init FAiled"
and in place of WSAGetLastError() it is giving error "0".i reffered to msdn there is no such thing as error 0</b>
|
|
|
|
|
if(a=SOCKET_ERROR)
should be
if(a==SOCKET_ERROR)
That's why it's a good idea to put constants first when comparing - then the compiler
will surely catch the error:
if(SOCKET_ERROR==a)
Also...
listen() returns an int, not a SOCKET.
accept() returns INVALID_SOCKET on an error, not SOCKET_ERROR.
Yes they may be the same binary value, but little errors like
that can multiply into problems
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am currently using Visual Studio .NET 2003. I am doing 3d drawing and I have encountered a problem with rotating the object using a rotation matrix. Once it is drawn in the canvas the object is deformed because the command dc.MoveTo and dc.LineTo converts the double and float values computed from the trigonometric functions into integer values. Is there a way to remedy this problem?
|
|
|
|
|
Hi, I little bit understood ur problem
Maybe you can generate some intermediate poits between original points in matrix by taking avg of two original pts etc connect them also.
I'm sorry if its not helpful..
--
"Programming is an art that fights back!"
|
|
|
|
|
Are you trying to use DirectX to do this within a MFC application? Is your object a Mesh, or what? And, finally, how did you generate this rotation matrix?
To really have control over 3D object display and movement, you should do this with DirectX. It takes awhile to set everything up, but, offers a highly evolved set of APIs to display graphics, offers incredible control over all aspects of display, and looks fabulous.
I also have Visual C++ NET 2003. Read through the available DirectX 8 documentation. There are many code examples that are included with the DirectX SDK. Also, you can use both managed and unmanaged versions of DirectX 9.
As a final note, this type of operation can be done without using DirectX, but, you should have a good understanding of all the trigonometry involved. And, you will be implementing alot of functions that already exist and are thoroughly tested and reliable.
This is: The MSDN Direct3D 9 Reference[^]. With DirectX you begin with, IDirect3DDevice9[^] instead of a MFC canvas.
This is: The Getting Started with DirectX 9[^] documentation at MSDN.
|
|
|
|
|
Thanks for the advice. I am using the MFC canvas, I didn't know I could use directX. I'll try using that. My object is a 3d figure drawn through the use of the CDC paint commands of MFC. The rotation matrix is just a formula , a matrix multiplied to the coordinates of the object in order to change the necessary coordinates when rotated at a certain angle. My problem lies with the dc.LineTo command because it only accepts integer values and the trigonometric computations are done using float values in order to preserve the accurate angle computations. The dc.LineTo command converts the float values to integer values hence rounding off the values into whole number. This is what deforms the figure when drawn because the coordinates are rounded off. Anyway, I'll try studying directx and I would be able to animate it using that tool. Thanks for the useful advice.
|
|
|
|
|
I have made a program that acts a little stranger:
- if the interface of Windows is the originally one installed:
(Left click on My Computer, Properties, Advanced, Performance, "Let Windows choose ...") the program crushes in Windows "style", the error been C0000005
- if I change the interface (Left click on My Computer, Properties, Advanced, Performance, Adjust for best performance) everything goes fine
Debugging the program I found some bad things that I have corrected.
The question is: why in second case the errors are not present?
The program is written in VC++6, w.SP5
36. When you surround an army, leave an outlet free.
...
Do not press a desperate foe too hard.
SUN-TZU - Art of War
|
|
|
|
|
Maybe running in debug mode and stepping will help to find which line cause exception?
--
"Programming is an art that fights back!"
|
|
|
|
|