C++ error C2065: 'Value' : undeclared identifier -Newbe - visual-studio-2010

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::

Related

unknown override specifier in CString?

I have created a class and having CString data member.
class Clistof
{
CString m_exe;
public:
Clistof(CString temp){m_exe = temp}
};
error : C3646 'exe': unknown override specifier
Any idea ?
Thanks,
You probably did not include a header that contains the definition for CString. Try including #include <atlstr.h> if you haven't done so already.
Also, please note that you forgot to add a ; after the assignment statement in your constructor: instead of Clistof(CString temp){m_exe = temp} you should write Clistof(CString temp) { m_exe = temp; }.

C++ iterator mismatch error

I am keeping track if instances of my class using std::vector to store pointers to all of the class objects. I'm wrapping things up and want to remove the pointer in the destructor... but I am getting the following error:
Brazos.cpp:15:89: error: cannot convert 'std::vector::iterator {aka __gnu_cxx::__normal_iterator >}' to 'const char*' for argument '1' to 'int remove(const char*)'
instanceAddress.erase(std::remove(instanceAddress.begin(), instanceAddress.end(), this) instanceAddress.end());
it seems I may need to dereference the iterator... Here is my code:
std::vector<Brazos*> Brazos::instanceAddress;
Brazos::Brazos(Mano mano)
{
instanceAddress.push_back(this);
_mano = mano;
}
Brazos::~Brazos(void)
{
instanceAddress.erase(std::remove(instanceAddress.begin(), instanceAddress.end(), this) instanceAddress.end());
}
You're missing a comma:
instanceAddress.erase(std::remove(instanceAddress.begin(), instanceAddress.end(), this), instanceAddress.end());
^
Also, the error message refers to int std::remove(const char*), so make sure you have #include <algorithm> for the correct std::remove.

C++11 Initializing a std::string member

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

Compiling QMap Iterator under OSX (Clang++)

I have a C++ Class CustomMap that implements QMap like so..
template <class Key, class T> class CustomMap : public QMap<Key, T>
Now, in a function:
template <class MK, class IK, class TK> static TK _at(MK *map, int idx){
...
QMap<int, Key>::Iterator a;
QMap<int, Key>::Iterator b;
...
}
However, multiple errors come about from just these few lines of code...
error: unknown type name 'Iterator'
virtual Iterator insert(const Key &key, const T &value)
error: expected ';' after expression
QMap<int, Key>::Iterator a;
^
;
error: expected ';' after expression
QMap<int, Key>::Iterator b;
^
;
I'm already doing #include <QMap> in the header the iterator is defined, and typedef iterator Iterator also occurs inside the Qt QMap object.
I have the exact same code compiling and running under Visual Studio using msvc compiler. The errors are appearing using Clang on OSX.
My setup involves:
OSX
Clang
Qt 5.4
All include folders are set and everything else builds fine.
Edit: If I define the Iterator as QMap<int, QString>::Iterator a instead of QMap<int,Key>::Iterator a, it removes the errors. Can this behaviour be explained? From what I see, it's a virtually identical implementation to the original QMap

cvCreateButton VS2010

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

Resources