|
Thanks for the quick answer Richard!
I tried to do this, but the problem I ran into is the following:
I have a sourcefile "program.cpp" with header "program.h" in which my classes are defined. I want to pass an object from "program.cpp" to "function1.cpp", by:
double^ function1result = function1(CType^ object)
In function1 I should now write the function as:
double^ function1(CType^ object){}
When I do that, I get an "undeclared identifyer" error. When I include the "program.h" header where the class is defined I get a "type redefinition" error. So my question is how to pass these objects in a way that such errors are avoided.
|
|
|
|
|
Arjen Tjallema wrote: When I include the "program.h" header where the class is defined I get a "type redefinition" error.
This means you are defining the type (presumably CType ) in more than one place in your source code. Without seeing more of your header and source file it's difficult to be more explicit.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
Thanks again! I'll put some bits of my code here to clarify my question.
The "program.h" header contains a class clGlobals:
public ref class clGlobals
{
public:
double^ airDensity;
double^ waterDensity;
double^ gravitation;
};
In my "program.cpp" source I create an instance, put some values in it and (try to) call function1:
clGlobals^ globals = gcnew clGlobals();
globals->airDensity = Convert::ToDouble(1.025);
globals->waterDensity = Convert::ToDouble(1025);
globals->gravitation = Convert::ToDouble(9.81);
double^ load = function1();
The "function1.h" header contains a declaration of the function:
double^ load(clGlobals^ global);
And the "function1.cpp" source contains the actual function:
double^ load(clGlobals^ global)
{
return load;
}
I hope this clarifies my question.
|
|
|
|
|
I don't quite get some of this syntax. What does function1() do and where is it defined? Why does function load() not return a double, or does it?
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
O, excuse me, I made a mistake in my previous post.
The "function1.h" header code should be:
double^ function1(clGlobals^ global);
And the "function1.cpp" source code should be:
double^ function1(clGlobals^ global)
{
double^ load;
return load;
}
I hope it is understandable now.
|
|
|
|
|
Arjen Tjallema wrote: I hope it is understandable now.
Yes, and after correcting this line from:
double^ load = function1();
to:
double^ load = function1(globals);
it compiles and runs fine. If you still have a problem then it is not within this code.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
That's strange, I get the following error when trying to compile:
function1.h(1) : error C2065: 'clGlobals' : undeclared identifier
I have put the four files in a new project to isolate it from the rest of my code, but still I get this error message. Do yo have any clue where to look for the problem?
|
|
|
|
|
The best way to resolve this is to add the following lines:
#include "program.h"
#pragma once
at the beginning of function.h . And add:
#pragma once
at the beginning of program.h .
This should ensure that the class definition for clGlobals is found in any file that includes function.h . Also the #pragma once statements ensure that the header files are processed once only per compilation unit, even if they are found in #include statements more than once.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
That indeed solves the problem.
Thanks a lot for your help!
|
|
|
|
|
Arjen Tjallema wrote: Thanks a lot for your help!
You're welcome; it's good to find the solution occasionally!
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
Hi,
I have developed a small application. But it gives the above error
It is declared in another class named "CPassword"
SqlConnection ^connect = gcnew SqlConnection();
connect->ConnectionString = ConfigurationManager::ConnectionStrings["SQLConnection"]->ConnectionString;;
SqlCommand ^cmd = gcnew SqlCommand();
cmd->CommandType = CommandType::StoredProcedure;
cmd->CommandText = "GetUserPassword";
DataSet ^dtSet = gcnew DataSet();
try
{
connect->Open();
SqlDataAdapter ^adp = gcnew SqlDataAdapter();
adp->SelectCommand = cmd;
cmd->Parameters->Add(gcnew SqlParameter("@user_id",SqlDbType::VarChar));
cmd->Parameters["@user_id"]->Value = userID;
adp->Fill(dtSet, "Password");
return dtSet;
}
And I am calling this function from another class
CPassword ^objPassword = gcnew CPassword();
DataSet ^dtSet = gcnew DataSet();
dtSet = objPassword->fn_ReturnPassword(txtUserId->Text);
Plese help....
Thanks to all
|
|
|
|
|
Can you post the entire CPassword function and the DataSet class...it would help to understand your problem
the problem can be in the try catch.....how do you implement the catch (not reported in the forum)
|
|
|
|
|
Thank u for ur reply.
Actually I solved my problem by not returning 'DataSet' object, it pass as a reference in function parameter.
|
|
|
|
|
Ok, so first off, I'm really new to C++ so please let me know if this is in the wrong forum. I'm trying to finish a small project using Visual Studio 2008 Professional and I'm running into an error:
"The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log for more detail".
So this is roughly what I'm doing:
1. Create a new Windows Forms Application project
2. Add some Labels (one of them is dynamic and displays the current time), some bitmap pictures, and a clickable hyperlink
3. Create a version.rc file to display version info on the compiled .EXE file.
4. After making sure it runs in Debug mode, I compile it in Release mode
The compiled .EXE file works just fine on my 64-bit Windows 7 box I'm developing on, and it works fine on several 32-bit Windows 7 machines I've tested. It also worked on an XP box I got my hands on. However, when I had some others here at work test it, they got the above error on a Windows Vista machine and on a Windows XP machine.
So what gives? How can I create a simple little app that we can launch on all the various newer (XP on up) operating systems without having to rely on a prerequisite add-on being installed? I'm just looking to create some basic user notification windows. Nothing too fancy.
Is there something I need to change to get this thing to work consistently on all the OSes? Or do I need to do something other than a Windows Forms Application?
Thanks!
|
|
|
|
|
|
I think I've got it working now. I ended up copying the files located in "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT" into the same folder as my .EXE and it now seems to be working on all the machines I've tested.
|
|
|
|
|
Instead of manually copuing the required CRT files, you might want to look into using a Setup and Deployment project. Right-click on the project and select Add Merge Module, and select the module corresponding to the CRT file you listed above. If there are any components your app depends on, you can add those to the project as well. When the user runs the generated setup.exe file, it will autoamtically install the prerequisites (including the merge module), and then install your app.
Regarding the prerequistes, there's a handy utility here[^] that allows you to build custom prerequisite packages for VS.
Dybs
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
Hi ,
I am new to VC++/CLI and have been struggling with creating an expandable property in the property grid. I can create an expandbale property with an ExpabdableObjectConverter and I can even customize its vaklue field by overriding the ToString() method but where I run into problems is with TypeConverter. I want to create a simple expandable property, say Person, that has subproperties LastName, FirstName and ID - ID is an int. I can't seem to understand if I need CanConvertFrom, ConvertFrom, CanConvertTo and ConvertTo. I am finding TypeConverter very difficult and would appreciate any help you may be able to provide. Would anyone kindly direct me to some info or an example on how to do this? Thank you very much in advance.
Regards,
Paul
The code I have so far is
ref class CustomPersonConverter;
[TypeConverter(CustomPersonConverter::typeid)]
ref class CustomPerson {
private:
String^ _FirstName;
String^ _LastName;
int _ID;
public:
CustomPerson(void) {}
[DisplayName("Person's first name")]
[Description("First Name"), Category("rcGroupBox Custom Parameters")]
[System::ComponentModel::NotifyParentProperty(true)]
property String^ FirstName {
String^ get() {return _FirstName;}
void set(String^ new_FirstName) {_FirstName=new_FirstName;}
}
[Description("Last Name"), Category("rcGroupBox Custom Parameters")]
[System::ComponentModel::NotifyParentProperty(true)]
property String^ LastName {
String^ get() {return _LastName;}
void set(String^ new_LastName) {_LastName=new_LastName;}
}
[Description("ID"), Category("rcGroupBox Custom Parameters")]
[System::ComponentModel::NotifyParentProperty(true)]
property int ID {
int get() {return _ID;}
void set(int new_ID) {_ID=new_ID;}
}
[System::Security::Permissions::PermissionSet(System::Security::
Permissions::SecurityAction::Demand, Name = "FullTrust")]
ref class CustomPersonConverter : public TypeConverter {
public:
virtual bool GetPropertiesSupported(ITypeDescriptorContext^ context) override {
return true;
}
virtual PropertyDescriptorCollection^ GetProperties(ITypeDescriptorContext^
context, Object^ value, array<Attribute^>^ attributes) override
{
return TypeDescriptor::GetProperties(CustomPerson::typeid);
}
virtual bool CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)
override {
if ( sourceType == String::typeid | sourceType == InstanceDescriptor::typeid )
{
return true;
}
return TypeConverter::CanConvertFrom( context, sourceType );
}
virtual bool CanConvertTo( ITypeDescriptorContext^ context, Type^
destinationType ) override {
if ( destinationType == String::typeid | destinationType ==
InstanceDescriptor::typeid )
{
return true;
}
return TypeConverter::CanConvertTo( context, destinationType );
}
/* This code performs the actual conversion from a Person to an InstanceDescriptor. */
virtual Object^ ConvertTo( ITypeDescriptorContext^ context, CultureInfo^ culture,
Object^ value, Type^ destinationType ) override {
if ( destinationType == InstanceDescriptor::typeid )
{
return;
}
return TypeConverter::ConvertTo( context, culture, value, destinationType );
}
};
};
|
|
|
|
|
Hi,
I'm new to C++/CLI and I am getting compiler errors in a DLL appliction I am writing....
The DLL is passed in a text string from the calling application which needs to be cast to an unsigned int....
unsigned int^ pointParam = reinterpret_cast<unsigned int^>(dictParameters[POINT_PARAM]);
Then I am using the integer value to create an array of double values
but I need to increase the value by 2 so I thought I could add it in the array declaration statement...
sagXArray = gcnew array<double>(pointParam + 2);
I am getting the following compile error...
1>.\StrandedWireSag.cpp(42) : error C2676: binary '+' : 'System::UInt32 ^' does not define this operator or a conversion to a type acceptable to the predefined operator
Why is the compiler unhappy about adding two values in a declaration?
Thanks
|
|
|
|
|
The compiler complains because you are trying to add a number with a reference (a reference is like a C++ pointer, but it "points" to a garbage collected object).
unsigned int ^ is a reference to an unsigned int , so you should dereference it to get the actual value:
sagXArray = gcnew array<double>(*pointParam + 2);
But this will not work if you pass a number as a string (ex. "135"), you have to convert the string to a number using the method int::Parse :
int pointParam = int::Parse(dictParameters[POINT_PARAM]);
sagXArray = gcnew array<double>(pointParam + 2); Note that in this case I'm not dereferencing pointParam because it is declared as an int .
Hope it helps!
|
|
|
|
|
Hi Ghydo,
So, "reinterpret_cast" will cast to a reference (pointer), while "int::Parse" casts to a value.
Is that correct?
Thanks for the help!
regards,
Bert
|
|
|
|
|
Hi Bert,
reinterpret_cast can cast to anything you want: to cast to a unsigned int you can use reinterpret_cast<unsigned int^> . (MSDN[^])
int::Parse is a method that takes a string and converts it (not cast) into its integer value (string "135" --> int 135).
If dictParameters[POINT_PARAM] is an array of unsigned int ^ your cast was correct and you should dereference pointParam to obtain the value, but if it is an array of string you have to convert it using int::Parse .
How is dictParameters defined?
I hope it is clearer!
|
|
|
|
|
I'm almost brand new to C++ so I'm hoping someone can help me out. I'm using Visual Studio 2008 to create a C++ Windows Form application, and I want to display the current time in place of some static text I was previously using as one of my Label controls. I've spent some time poking around and I think I'm missing something basic.
I was trying something like this:
this->label1->Text = System::DateTime::Now;
and something a little more complexe like this:
this->label1->Text = (System::DateTime::Now.ToString("T"));
But no matter how I manipulate the line, either the code editor or the design editor within Visual Basic Studio barks at me. On my first example shown above, the code editor tells me it "....cannot convert parameter 1 from 'System::DateTime' to 'System::String ^'" while the design editor is just fine. And on my second example shown above, the design editor tells me "C++ CodeDOM parser error:Line:76, Column:56 --- Internal Error" while the code editor is just fine.
So can someone help me out here and let me know what I'm missing? My goal is to just have something like "5:11:45 PM" show up as the text of my Label control when a user runs the program.
Thanks!
|
|
|
|
|
I would start by dropping the parentheses around the righthand side.
|
|
|
|
|
If I change it to this: this->label1->Text = System::DateTime::Now.ToString("T");
I get the same error in the design editor as when it's set to this: this->label1->Text = (System::DateTime::Now.ToString("T"));
I should also mention, that as long as the code editor doesn't have a problem, when I run the compiled .EXE it works just fine. But I'm leary of ignoring an error in the design editor since this is kinda of my first C++ project.
|
|
|
|