I have MFC dialog based application.
void CThr_MfcDlg::OnBnClickedButton1()
{
this->SetWindowTextW(L"bla");
(CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello") ;
}
Line this->SetWindowTextW(L"bla"); changes form caption to bla
I expect line (CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello") ; should change caption to hello, but have compile error:
Error 1 error C2440: 'type cast' : cannot convert from 'void' to 'CThr_MfcDlg *'
Read this. Since -> operator's precedence (2) is higher than cast operator's (3), your code is parsed in this way:
(CThr_MfcDlg*) (GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello")) ;
To avoid this, you should use parenthesis with casting.
// this will be correct.
((CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG))->SetWindowText(L"hello");
Related
Part of my code:
double function (double x)
{
f = x^5-3*x^4+3*x^3-2*x^2-5;
return f;
}
Problem: I am getting following errors for this part of the code:
error C2296: '^' : illegal , left operand has type 'double'
error C2297: '^' : illegal , right operand has type 'double'
My Goal: I am writing a code to find the roots of the following polynomial in C++, Visual Studio 2012:
I am not sure how to solve this error since I am learning C++ and this is my first time I encountered this error. There are only two aforementioned errors; rest of my code is error-free. Your help will be much appreciated. Thank you!
The ^ operator in C/C++ is not an exponent operator (by default) - it's a bitwise XOR operator, and doesn't work on doubles.
Instead, use the pow function.
maybe you would know, I get an error:
error C2440: 'initializing' : cannot convert from 'int'
Conversion from integral type to pointer type requires reinterpret_cast
It goes to ' file in MS VS 2010 folder:
template<class _Other1,
class _Other2>
_Pair_base(_Other1&& _Val1, _Other2&& _Val2)
: first(_STD forward<_Other1>(_Val1)),
second(_STD forward<_Other2>(_Val2))
{ // construct from moved values
}
I was looking for different solutions but could not find a correct one.
The error says
'initializing' : cannot convert from 'int' to 'EnterFunctor *'
The only part of your code you share is
functors.push_back(make_pair(sessionStartFunc,
pair<EnterFunctor*, ExitFunctor*>(NULL,sessionStartExit)));
If NULL is #defined as 0 this gives you an int but you promised a pair of pointers, so as the next line of the error says you can use a cast to make NULL the right type of pointer.
So, I am trying to use the following code to pass an object to a function in Qt, then convert it to a QLabel for further processing (it's part of an animation sequence):
void myAnimation(QObject* label)
{
QLabel *lbl = qobject_cast<QLabel*>label;
//more code.....
}
Yet, whenever I try to compile, I get the following two errors:
error: C2440: 'initializing' : cannot convert from 'overloaded-function' to 'QLabel *'
Context does not allow for disambiguation of overloaded function
and
error: C2146: syntax error : missing ';' before identifier 'label'
Why isn't my code working? Any help is appreciated. Thank you!
Try:
QLabel *lbl = qobject_cast<QLabel*>(label);
// ^ ^
I am writing a simple C++ database, using Visual Studio 2008 express edition, like program to sort and search a text file containing NCAA winners and runner ups by year, this data is to be saved within a structure. I understand how to do the sorting and searching but i am having trouble with properly initializing the structure and passing it into a function, as when i try to do it how i was shown in class i get several errors that i have been unable to get rid of, any help would be greatly appreciated.
The errors are as follows;
project5.cpp(145) : error C2676: binary '[' : 'Data' does not define this operator or a conversion to a type acceptable to the predefined operator
project5.cpp(145) : error C2228: left of '.year' must have class/struct/union
project5.cpp(146) : error C2676: binary '[' : 'Data' does not define this operator or a conversion to a type acceptable to the predefined operator
project5.cpp(146) : error C2228: left of '.schoolWin' must have class/struct/union
project5.cpp(147) : error C2676: binary '[' : 'Data' does not define this operator or a conversion to a type acceptable to the predefined operator
project5.cpp(147) : error C2228: left of '.scoreWin' must have class/struct/union
project5.cpp(148) : error C2676: binary '[' : 'Data' does not define this operator or a conversion to a type acceptable to the predefined operator
project5.cpp(148) : error C2228: left of '.schoolRunnerUp' must have class/struct/union
project5.cpp(149) : error C2676: binary '[' : 'Data' does not define this operator or a conversion to a type acceptable to the predefined operator
project5.cpp(149) : error C2228: left of '.scoreRunnerUp' must have class/struct/union
project5.cpp(191) : error C2664: 'initializeStructure' : cannot convert parameter 1 from 'Data [100]' to 'Data &'
My Structure Declaration:
const int Size = 100; // size of structure
struct Data { // Struct definintion to take in stats
int year;
string schoolWin;
int scoreWin;
string schoolRunnerUp;
int scoreRunnerUp;
} NCAAStats [Size] ;
void initializeStructure(Data& NCAAStats, int Size); // initialization function prototype
//function declaration
void initializeStructure(Data& NCAAStats, int Size) {
int Idx;
for (Idx = 0; Idx < Size; Idx++) {
NCAAStats[Idx].year = 0000;//line145
NCAAStats[Idx].schoolWin = "winning school";//line146
NCAAStats[Idx].scoreWin = 000;//line147
NCAAStats[Idx].schoolRunnerUp = "losing school";//line148
NCAAStats[Idx].scoreRunnerUp = 000;//line149
}
}
//initalize the array of structures
initializeStructure(NCAAStats, Size);//line 191 function call from within main
Based off the last error i was thinking that it is possible that for some reason visual studio thinks my structure is named Data with a size of 100 when, it is NCAAStats of size 100, but i am not sure what i did wrong that is causing this, any suggestions would be greatly appreciated.
Since you tagged C++ I'd suggest you use C++ features...
#include <vector>
#include <string>
struct Data
{
unsigned int year;
std::string schoolWin;
unsigned int scoreWin;
std::string schoolRunnerUp;
unsigned int scoreRunnerUp;
Data()
: year(0)
, schoolWin("winning school")
, scoreWin(0)
, schoolRunnerUp("loosing school")
, scoreRunnerUp(0){}
}
// In main
std::vector<Data> my_data(100); // Create and initialize 100 instances of "Data".
Note this error:
project5.cpp(191) : error C2664: 'initializeStructure' : cannot convert parameter 1 from 'Data [100]' to 'Data &'
The argument NCAAStats you defined for initializeStructure() is a reference to one struct of type NCAAStats, which you can't use like an array (I assume that's what you were going for). You'll have to change it to a pointer (Data * or Data[]) to match the type you're actually passing as an argument.
You have declared an array NCAAStats[Size] at the global scope. However, your functions have arguments called NCAAStats; these hide the global definition. Therefore, stuff like NCAAStats[Idx] is invalid, because that NCAAStats is a Data &, not a Data[].
I've been trying to get back into coding for a while, so I figured I'd start with some simple SDL, now, without the file i/o, this compiles fine, but when I throw in the stdio code, it starts throwing errors. This I'm not sure about, I don't see any problem with the code itself, however, like I said, I might as well be a newbie, and figured I'd come here to get someone with a little more experience with this type of thing to look at it.
I guess my question boils down to: "Why doesn't this compile under Microsoft's Visual C++ 2008 Express?"
I've attached the error log at the bottom of the code snippet. Thanks in advance for any help.
#include "SDL/SDL.h"
#include "stdio.h"
int main(int argc, char *argv[])
{
FILE *stderr;
FILE *stdout;
stderr = fopen("stderr", "wb");
stdout = fopen("stdout", "wb");
SDL_Init(SDL_INIT_EVERYTHING);
fprintf(stdout, "SDL INITIALIZED SUCCESSFULLY\n");
SDL_Quit();
fprintf(stderr, "SDL QUIT.\n");
fclose(stderr);
fclose(stdout);
return 0;
}
Actual errors reported:
main.cpp(6) : error C2090: function returns array
main.cpp(6) : error C2528: '__iob_func' : pointer to reference is illegal
main.cpp(6) : error C2556: 'FILE ***__iob_func(void)' : overloaded function differs only by return type from 'FILE *__iob_func(void)'
c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(132) : see declaration of '__iob_func'
main.cpp(7) : error C2090: function returns array
main.cpp(7) : error C2528: '__iob_func' : pointer to reference is illegal
main.cpp(9) : error C2440: '=' : cannot convert from 'FILE *' to 'FILE ***'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(10) : error C2440: '=' : cannot convert from 'FILE *' to 'FILE ***'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(13) : error C2664: 'fprintf' : cannot convert parameter 1 from 'FILE ***' to 'FILE *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(15) : error C2664: 'fprintf' : cannot convert parameter 1 from 'FILE ***' to 'FILE *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(17) : error C2664: 'fclose' : cannot convert parameter 1 from 'FILE ***' to 'FILE *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(18) : error C2664: 'fclose' : cannot convert parameter 1 from 'FILE ***' to 'FILE *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
#include "SDL/SDL.h"
#include "stdio.h"
int main(int argc, char *argv[])
{
FILE *stderr; //Invalid names. These are already defined by stdio.h.
FILE *stdout; //You can't use them (portably anyway).
stderr = fopen("stderr", "wb"); //I'm assuming you actually want files
stdout = fopen("stdout", "wb"); //called "stderror" and "stdout".
SDL_Init(SDL_INIT_EVERYTHING);
fprintf(stdout, "SDL INITIALIZED SUCCESSFULLY\n");
SDL_Quit();
fprintf(stderr, "SDL QUIT.\n");
fclose(stderr);
fclose(stdout);
return 0;
}
Try changing the names stderr and stdout to something else. I suspect the compiler is complaining because these are already defined elsewhere in the C library.
You don't need to declare, open, or close stdin, stdout or stderr, that's already done for you in stdio.
If you're using C++, why not iostream?