|
Probably crashing because you're attempting to create a class from code you never imported.
|
|
|
|
|
Thanks for your reply.
In my project's cpp file from where I will call function from DLL, I add the following lines at the file top:
__declspec(dllimport) class Class1;
__declspec(dllimport) class Class2;
__declspec(dllimport) class Utility;
but still get the same error.
|
|
|
|
|
I test the program, the reason for the error is that I pass std::string as argument to Class1 constructor.
Class1(std::string arg1, std::string arg2){
this.ProjDir = arg1.data();
this.ProjName = arg2.data();
}
calling side:
std::string dir = ".....";
std::string name = "....";
Class1 obj = new Class1(dir name);
I test if I call empty constructor, it's fine.
What should I do now? I need to pass these value to Class1.
|
|
|
|
|
Try using the c_str() [^] member function to get the data from the string parameters.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Richard, thanks for your suggestion.
Yes, if in the calling side I pass string.c_str() value
string xx = "calling...";
func(xx.c_str());
and in the function definition side I define function as:
func(const char* str){}
everything is fine.
But my project has hundreds functions, it was created in C++ 6.0, now I wish to migrate it to VS2008, if I have to modify all the function definition and modify all the places to function call, that's a big work.
Is there any better way to solve this issue? Thanks!
|
|
|
|
|
I do not know of any simple way other than "Find & Replace in Files" (Ctrl+Shift+H). It's a pity that you created the functions to accept char* parameters rather than string object references, then you would not have had to change in so many places.
[edit]
It is, of course, always possible, that the 'bug' in your code will not be fixed by these changes, but is connected to something else. Make sure that you are actually fixing the right problem before you make all these changes.
[/edit]
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I am testing to pass std::string by reference instead of std::string, it works fine. so in this case, all I need to do are modify all the function siguratures with std::string as argument, that saves a lots of time.
Thanks!
|
|
|
|
|
I also just noticed that in one of your earlier posts you are using std::string s for string constants. This is a waste of time as you are just adding extra code to copy the constant character value into a std::string in order to pass it to some function.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Yes, this issue is caused by pass std::string between EXE and DLL.
When I search the solution from internet, all say that you canno pass std::string between DLLs, you'd better pass C-style character string, that's way I convert std::string to char*.
Now since passing std:: string by reference can solve the issue, that's best.
|
|
|
|
|
Andraw111 wrote: this issue is caused by pass std::string between EXE and DLL. I'm sure it's more than just that.
Andraw111 wrote: you canno pass std::string between DLLs If that were true you would not be able to pass any objects between DLLs.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Looks like you're crashing because of heap corruption. The crash is almost certainly happening in the "Class1* obj = new Class1(); " line (it's common for the line number to indicate the line after actual call in question), the "dbgheap.c" all but proves it.
With heap problems the actual root cause (when the heap got corrupted) and the resulting crash (in your case when the heap manager detects it) can be separated but quite some time, which makes it difficult to debug.
I'll take a wild quess at the cause however:
Looks like the new statement with allocates the object is in the DLL. How is it delete d? By a delete statement in the DLL's client? If so try moving it into the DLL where it belongs:
void DeleteCase1Obj(Class1 *obj)
{
delete obj;
}
Replace all delete s of this object in the client with calls to this function which is exported from the DLL.
Steve
|
|
|
|
|
Hi, dear all,
I have a DLL file created in C++ 6.0, now move to VS2008 and get compile error.
in header file:
#define XXXPORT __declspec(dllimport)
template <class TP> class XXXPORT TPtrList
{
public:
int entries(bool deleted = false) const;
TP *operator[](int n);
const TP *operator[](int n) const;
..............
..............
}
in .cpp file
template <class TP>
inline int TPtrList<TP>::entries(bool deleted) const
{
.........
}
template <class TP>
inline TP *ABSTPtrList<tp>::operator[](int n)
{
.........}
In C++, they are fine, but in VS2008, When I compile it, I got
error C2491: 'TPtrList<TP>'::entries' : definition of dllimport function not allowed
error C2491: 'TPtrList<TP>::operator []' : definition of dllimport function not allowed
What cause this problem? Thanks!
|
|
|
|
|
This looks like you're defining the functions when doing the dll import... when all you should have is function prototypes. You probably want to define XXPORT up there to say dllexport.
|
|
|
|
|
Yes, you are right.
I just change to dllexport, then I can add function implementation to it, so can be used by other class inside this DLL.
Thanks!
|
|
|
|
|
Hi, dear all,
I create a class name Point2D to store coordinate value, with "<" and "==" two operators overloadings.
Then create a type based on it as:
typedef std::vector<Point2D*> Point2D_Vector;
Point2D_Vector PointVector;
Then add some data to PointVector.
When I loop the vector:
Point2D_Vector::iterator itr;
for (itr=PointVector.begin(); itr!=PointVector.end(); itr++)
if (itr != 0)
delete *itr;
They work fine in C++6.0, now when I try to compile it in VS2008 I got the error message in if(itr != 0) line:
error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
How can I solve this issue? Thanks!
|
|
|
|
|
|
The statement itr != 0 makes no sense. Even when it compiled in VC6 it wasn't doing what it looks like you intended (to stop deleting a NULL pointer). You probably mean this:
if (*itr != 0)
Also, deleting a NULL pointer is safe in C++ (it does nothing). The check is not needed and it potentially makes the code less efficient as the compiler inserts code to check for this automatically.
Steve
|
|
|
|
|
Thanks, Chris and Steve, it can be compiled now.
|
|
|
|
|
a code to change string to uppercase using integer values within the code.
|
|
|
|
|
There are built in library and MFC functions to perform this. Please explain what you mean by "change string to uppercase using integer values"?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hint: There's an offset of 32 between lower and upper cased letters. For example, the ascii value (int) of lower case 'c' is 99, and the same of upper case 'C' is 67.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
The integer difference between a lowercase char and a uppercase char when subtracted is 32.
simply subtract the lowercase by 32 to get a uppercase char.
#include <stdio.h>
#include <string.h>
int main(void)
{
char strchr[32] = "the quick fox";
int strlength=strlen(strchr);
printf("Before\n" );
printf("%s\n\n", strchr);
for (int i=0; i<strlength; i++)
{
if((strchr[i] >= 97) && (strchr[i] <= 122))
{
strchr[i] = strchr[i] -32;
}
}
printf("After\n" );
printf("%s\n\n", strchr);
return(0);
}
|
|
|
|
|
Using hexadecimal rather than decimal integer values makes the code much more readable.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
TopCoder23 wrote: char strchr[32] = "the quick fox"; I would consider a different variable name as strchr is the name of a CRT function.
TopCoder23 wrote: if((strchr[i] >= 97) && (strchr[i] <= 122)) What about:
if (islower(strchr[i]))
strchr[i] = toupper(strchr[i]);
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|