When I use the code
cvCreateButton("button1",callbackButton2,NULL,CV_PUSH_BUTTON,1);
void callbackButton2(int state, void *pointer){
int i = 2;
return;
}
I get the following error in visual studios 2010
error C2065: 'callbackButton2' : undeclared identifier
Could someone please explain what I am doing wrong to cause this error
Thanks
Based on the code presented it appears that callbackButton2 is used on the first line but is declared after this on the second line.
In most languages this is not a legal thing to do because an identifier is not valid until the compiler can determine the context(s) in which it can be used which is what declaring an identifier does
Try this:
void callbackButton2(int state, void *pointer){
int i = 2;
return;
}
cvCreateButton("button1",callbackButton2,NULL,CV_PUSH_BUTTON,1);
In that code sample the identifier callbackButton2is declared as a function before being used as an argument to the cvCreateButton function and should therefore be a valid use of the identifier
Related
I have a function definition like this:
double getPriceTimeByPtr(const StrategyParams* ptr, const int applied_price, const int timeframe, const int shift, const int shift_delta, const bool normalized, time_t &time);
but when I compile this code it errors just before the &time part so clearly there is a problem passing a time_t object by reference.
How do I fix this please?
I recently added the time_t parameter to the function and the error occurred since then.
Here's the errors generated:
Severity Code Description Project File Line Suppression
State
Error C2143 syntax error: missing ')' before '&'
Error C2143 syntax error: missing '{' before '&'
Error C2059 syntax error: '&'
Error C2059 syntax error: ')'
The syntax seems correct to me but the compiler doesn't like it.
The project that was failing was defined as a C project which was then trying to call a C++ function by reference. C can't us by reference so was failing. Changing the time parameters to pointers worked.
suppose I have a class like this and I would like to create a string with a
specific capacity. I tried doing the following but that did not work.
Any suggestions ? I know I could do it in the constructor but would like to do it during the declaration if possible.
class foo
{
std::string bar = std::string().resize(45);
}
I get the error
main.cpp: In function 'int main()':
main.cpp:8:46: error: conversion from 'void' to non-scalar type 'std::__cxx11::string {aka std::__cxx11::basic_string}' requested
std::string test = std::string().resize(45);
In C++, you probably don't want to "chain" methods unless previous methods have the correct return type.
As suggested by #James Maa, you can do simply use the constructor.
In c++11 we have new feature called move constructor so
string str = string();
doesn't cause extra time.
http://en.cppreference.com/w/cpp/language/move_constructor
with move constructor, the program would directly use the address of the temporary constructed string after = sign, without making a copy.
The problem is that resize()function in c++ actually returns void
basic_string( size_type count,
CharT ch,
const Allocator& alloc = Allocator() );
This constructor might be something you are directly interested in.
You can do something with
std::string str(45, ' ');
When I initialize the std::string member of a class calling its C string constructor, I receive the following errors:
error: expected identifier before string constant
error: expected ',' or '...' before string constant
Although, the program compiles successfully when I use copy initialization or list initialization.
class Element
{
private:
std::string sName_("RandomName"); // Compile error
std::string sName_ = "RandomName"; // OK
std::string sName_{"RandomName"}; // OK
}
What seems to be the problem?
UPDATE
Now I realize this is a stupid question, because, as #p512 says, the compiler will see it as a erroneous method declaration. But I think this question should remain for other people that will do the same thinking error.
std::string sName_("RandomName");
This is an erroneous function declaration - at least that's what the compiler makes of it. Instead you can use something like this:
std::string sName_ = std::string("RandomName");
You can also use initializer lists in the constructor of your class:
class A {
public:
A() : sName_("RandomName") {}
std::string sName_;
};
You can find more on that here: http://en.cppreference.com/w/cpp/language/initializer_list
Hi I am new to Visual Studio 2010
When I try build my code it tells me:
count.cpp(18): error C2065: 'Value' : undeclared identifier
That is my count.cpp code giving out the error:
#include "StdAfx.h"
#include "count.h"
count::count(void): Value(0), ResetValue(0){} //constructor
count::~count(void){} //destructor
int GetValue(){return Value;} //Accessor to get value by another class
Here is my count.h code:
#pragma once
class count
{
public:
count(void);
virtual ~count(void);
int GetValue();
private:
int Value;
int ResetValue;
};
It's a count object that's going to be used by a countWindow object.
So I want an "GetValue" accessor and make use of a pointer in my countWindow dialog window.
Is the variable Value not declared and initiated on the line 12 when I actually make use of the constructor?
Or is it not link to the declaration in the count.h file?
Because if I create another void function to just do a Value++, Visual studio seems fine with.
Thank you for your time!
GetValue is being declared as a global not a member of the count class. Preface the GetValue declaration with count::
int count::GetValue(){return Value;} //Accessor to get value by another class
int count::GetValue(){return Value;} //Accessor to get value by another class
Note the count::
GCC allows customization of printf specifiers. However, I don't see how I can "teach" it to accept my string class for %s specifier. My string class is a simple wrapper over char pointer and has exactly one member variable (char * data) and no virtual functions. So, it's kind of ok to pass it as-is to printf-like functions in place of regular char *. The problem is that on gcc static analyzer prevents me from doing so and I have to explicitly cast it to const char * to avoid warnings or errors.
My cstring looks something like this:
class cstring
{
cstring() : data(NULL){}
cstring(const char * str) : data(strdup(str)){}
cstring(const cstring & str) : data(strdup(str.data)){}
~cstring()
{
free(data);
}
...
const char * c_str() const
{
return data;
}
private:
char * data;
};
Example code that uses cstring:
cstring str("my string");
printf("str: '%s'", str);
On GCC I get this error:
error: cannot pass objects of non-trivially-copyable type 'class cstring' through '...'
error: format '%s' expects argument of type 'char*', but argument 1 has type 'cstring' [-Werror=format]
cc1plus.exe: all warnings being treated as errors
The C++ standard doesn't require compilers to support this sort of code, and not all versions of gcc support it. (https://gcc.gnu.org/onlinedocs/gcc/Conditionally-supported-behavior.html suggests that gcc-6.0 does, at least - an open question whether it will work with classes such as the one here.)
The relevant section in the C++11 standard is 5.2.2 section 7:
When there is no parameter for a given argument, the argument is passed in such a way that the receiving function can obtain the value of the argument by invoking va_arg ...
Passing a potentially-evaluated argument of class type (Clause 9)
having a non-trivial copy constructor, a non-trivial move constructor,
or a non-trivial destructor, with no corresponding parameter, is
conditionally-supported with implementation-defined semantics.
(But look on the bright side: if you get into the habit of using c_str, then at least you won't get tripped up when/if you use std::string.)