constructor input error in VC++ - visual-studio-2010

I create a constructor as follow
Form1(array<System::String ^> ^args) //HW5
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
if (args->Length==0){
CregArray = gcnew array<CRegistration^>(100);
record_number = 0;
}
else {
}
}
After that I use a line of code to create the constructor. Basically, I want to use the case of length==0, but the compiler said there is an error. I don't understand what the compiler means.
Application::Run(gcnew Form1(""));
The error is "Error 1 error C2664: 'Project3::Form1::Form1(cli::array ^)' : cannot convert parameter 1 from 'const char [1]' to 'cli::array ^'

You are passing in a string where an array of strings is expected.
Also, String::Empty is better practice than using a literal empty string.
Try this:
array<System::String^>^ args = gcnew array<System::String^>(1);
args[0] = String::Empty;
Application::Run(gcnew Form1(args));

Related

Passing a temporary stream object to a lambda function as part of an extraction expression

I have a function which needs to parse some arguments and several if clauses inside it need to perform similar actions. In order to reduce typing and help keep the code readable, I thought I'd use a lambda to encapsulate the recurring actions, but I'm having trouble finding sufficient info to determine whether I'm mistakenly invoking undefined behavior or what I need to do to actualize my approach.
Below is a simplified code snippet of what I have currently:
int foo(int argc, char* argv[])
{
Using ss = std::istringstream;
auto sf = [&](ss&& stream) -> ss& {
stream.exceptions(ss::failbit);
return stream;
};
int retVal = 0;
bool valA = false;
bool valB = false;
try
{
for(int i=1; i < argc; i++)
{
std::string arg( argv[i] );
if( !valA )
{
valA = true;
sf( ss(arg) ) >> myInt;
}
else
if( !valB )
{
valB = true;
sf( ss(arg) ) >> std::hex >> myOtherInt;
}
}
}
catch( std::exception& err )
{
retVal = -1;
std::cerr << err.what() << std::endl;
}
return retVal;
}
First, based on what I've read, I don't think that specifying the lambda argument as an rvalue reference (ss&&) is doing quite what I want it to do, however, trying to compile with it declared as a normal reference (ss&) failed with the error cannot bind non-const lvalue reference of type 'ss&'. Changing ss& to ss&& got rid of the error and did not produce any warnings, but I'm not convinced that I'm using that construct correctly.
I've tried reading up on the various definitions for each, but the wording is a bit confusing.
I guess ultimately my questions are:
Can I expect the lifetime of my temporary ss(arg) object to extend through the entire extraction expression?
What is the correct way to define a lambda such that I can use the lambda in the way I demonstrate above, assuming that such a thing is actually possible?

Regular expression executing before the other one

i am having a problem with my code
alphabet [a-zA-Z]
var {alphabet}+[{chiffre}{alphabet}_]*
{varIntDecl} {
for(int i=5 ;i<yyleng;i++)tab[cmp].token[i-5]=yytext[i]; strcpy(tab[cmp].value,"0");strcpy(tab[cmp].type,"Var");
tab[cmp].indiceTS= cmp;
strcpy(tab[cmp++].typeValeur,"int");
yylval.indiceTS=cmp;
return (INTVARIABLEDECL);
}
{var} {
for(int i=0;i<cmp;i++){
if(!strcmp(tab[i].token,yytext)) {
return (VARINT);
}
}
printf("Error Variable non trouvé Please Declare First ");
}
varIntDecl {int}{blancs}{var};
when i'm executing this
i have a yacc part but it's irrelevant to this
i'm getting this conflict of var executing everytime even tho it's not what i wanted to do
here is the result of execution:
int abc
Error Variable non trouvé Please Declare First
int adc
Error Variable non trouvé Please Declare First

Explicit unique Handle Modern c++

I am following along a course about modern C++. I get an error with explicit unique_handle function. It says m_value is unknown override specifier? I listed the value above it shows still be in variable space.
explicit unique_handle(pointer value = Traits::invalid()) throw()
m_value { value }
{
}
https://pastebin.com/MA4vnWR3
You are missing a colon ':' before m_value, which denotes the start of the inializer list:
explicit unique_handle(pointer value = Traits::invalid()) throw()
: m_value { value }
{
}

unable to use optional int "possibleNumber" in optional binding

I'm new to Swift and is trying to learn the concept of optional binding. I have came up with the following code:
let possibleNumber = Int("123")
possibleNumber.dynamicType
if let actualNumber = Int(possibleNumber){
print("\(possibleNumber) has an integer value of \(actualNumber)")
} else {
print("\(possibleNumber) could not be converted to an int")
}
Xcode playground output error message:
value of optional type "int?" not unwrapped, did you mean to use "!" or "?"
However, when I added the "!" to if let actualNumber = Int(possibleNumber!){
let possibleNumber = Int("123")
possibleNumber.dynamicType
if let actualNumber = Int(possibleNumber!){
print("\(possibleNumber) has an integer value of \(actualNumber)")
} else {
print("\(possibleNumber) could not be converted to an int")
}
Xcode display another error message:
initialiser for conditional binding must have Optional type, not int
Why is this happening?
The result of
let possibleNumber = Int("123")
is an optional Int - Int?
Then you're trying to create another Int with
Int(possibleNumber)
which does not work because the initializer expects a non-optional type.
The error message is related to the initializer rather than to the optional binding.
Try this to get the same error message.
let possibleNumber = Int("123")
let x = Int(possibleNumber)
In your second example when you initialize an Int with an implicit unwrapped Int! argument you get a non-optional Int and the compiler complains about the missing optional.
In the if let construct
if let actualNumber = Int(possibleNumber!){
print("\(possibleNumber) has an integer value of \(actualNumber)")
}
you don't need to use the Int initializer. You simply need to write
if let actualNumber = possibleNumber {
print("\(possibleNumber) has an integer value of \(actualNumber)")
}
Now Swift will try to unwrap possibleNumber. If the operation does succeed the unwrapped value is put inside actualNumber and the THEN block executed.

How to define an array of strings in C++/CLI?

What's wrong in this:
i'm getting these errors for all the 5 definitions:
error C3698: 'System::String ^' : cannot use this type as argument of 'gcnew'
error C2512: 'System::String::String' : no appropriate default constructor available
array<String^>^ arr = gcnew array<String^>
{
gcnew String^ "Madam I'm Adam.",
gcnew String^ "Don't cry for me,Marge and Tina.", //error C2143: syntax error : missing '}' before 'string' AND error C2143: syntax error : missing ';' before 'string'
gcnew String^ "Lid off a daffodil.",
gcnew String^ "Red lost Soldier.",
gcnew String^ "Cigar? Toss it in a can. It is so tragic."
}
You should not use gcnew inside the array initializer:
array<String^>^ arr = gcnew array<String^> {
"Madam I'm Adam.",
"Don't cry for me,Marge and Tina.",
"Lid off a daffodil.",
"Red lost Soldier.",
"Cigar? Toss it in a can. It is so tragic."
};
The other answerer has the correct syntax, but it's not because you're in an array initializer.
There's two errors with your string initialization.
When using gcnew, you don't need to include the ^. You're
constructing a new object, not a new reference.
You need parentheses when calling the constructor.
So the proper constructor syntax would be to call gcnew String("Madam I'm Adam.").
However, as other answerer noted, you don't need to do that. The string literal is already a String object, so you can remove the call to the constructor and just use the string literal directly. This is the same as calling new String("Madam I'm Adam.") in C#: It's already a string object, calling new String is redundant.

Resources