cout in Visual Studio 2010 - visual-c++

When I attempt to compile the following
#include <iostream>
using namespace std;
#include "stdafx.h" // This was included by Visual Studio
int _tmain(int argc, _TCHAR* argv[]) // The name _tmain was generated by Visual Studio
{
int a = 1;
cout << a << "\n";
return 0;
}
I get a compiler message:
warning C4627: '#include <iostream>': skipped when looking for precompiled header use
Add directive to 'StdAfx.h' or rebuild precompiled header
Then I'm told that cout is undefined. (It doesn't help to write std::cout.)
I'm using a default Visual Studio projects. This is the first time I've used this. Suggestions appreciated.

#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a=10;
cout << a << "\n";
cin>>a;
return 0;
}
Slam dear! I have written the same code, but only changing the order of first three line. It gives result at console without any error or warning. Please check it.

Put your iostream include and the std namespace declaration after the stdafx.h include. The program will then compile and run.
As to why, my guess is that precompiled headers (enabled by default) rely on the exact sequence of #include directives. Putting iostream first means that the PCH for stdafx no longer matches the actual sequence of declarations known to the compiler at that point.

Related

Uninitialized local variable 'state' used

So I want to create a program which allows users to map buttons to keyboard presses using c++ with Visual Studio 2015. I have been having a ton of trouble with Xinput and I was hoping someone could help me with one simple problem which makes no sense seeing as I have defined it.
So my problem is I get one error which says unresolved external symbol _XinputGetState#8 referenced in function _main.
Here is my code:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <Xinput.h>
using namespace std;
int main() {
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
if (XInputGetState(0, &state) == ERROR_SUCCESS)
{
cout << "It worked!" << endl;
}
bool A_button_pressed = ((state.Gamepad.wButtons & XINPUT_GAMEPAD_A) != 0);
cout << A_button_pressed << endl;
}
In general unresolved external symbols means that a library needed for the function is not linked.
In this case:
XInputGetState() requires XInputLib.lib and Xinput9_1_0.lib.
This can be resolved by adding the libraries in the project settings or via:
#pragma comment(lib,"XInput.lib")
#pragma comment(lib,"Xinput9_1_0.lib")

SDL2 Threads C++ pointer corruption

So, i have the following problem which may seem pretty strange or too elementary. This code snippet demonstrates my problem.
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include "SDL2/SDL.h"
#include <iostream>
using namespace std;
int doSTH(void* data){
int* data2 = (int*)data;
cout << data2 << endl;
return 0;
}
int main(){
SDL_Init(SDL_INIT_EVERYTHING);
int* data = new int(2);
cout << data << endl;
SDL_CreateThread(doSTH, "sth", (void*)data);
SDL_Delay(1);
delete data;
SDL_Quit();
}
Output is
0x2479f40
0x400c05
That means that somehow the function i call doesn't get the pointer i give it, am i missing something?
I am using Linux Ubuntu 14.04, g++ 4.8 and codeblocks.
Please tell me if i should give any more info.
Thanks in advance.
Nevermind, somehow the build of SDL2 was screwed up. I just uninstalled libx11-dev, rebooted and then reinstalled libsdl2-dev and now it works correctly.

Visual C++ versus standard c++

1) I have been working with standard C++ (CodeBLocks)and starting to move to Visual C++. When creating a console application the VS builds the following:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
This is not the standard c++ syntax but windows version.
Now, is there a way to use the standard c++ syntax in Visual Studio C++ and avoid the above
sintax so as to use the standard?
I mean, using VS C++ be able to code something standard such as:
#include "<iostream.h>"
#include <stdlib.h>
int main()
{
int month, day, year;
cout << "Hellow World" << endl;
return 0;
}
2) I get in visual c++ error by trying to include very common libraries such as
#include "<iostream.h>". Any advise much appreciated. (using VS 2013 and comparing it with previous code in WnDEv).
3) I also attemted to use this with an empty project adding:
but when I build it InteliSense cannot open source file stdafx.h, IntelliSense identifier "cout" is undefined, IntelliSense identifier "cin" is undefined. Please help. thank you
#include "stdafx.h"
#include<iostream>
#include<conio.h>
void main()
{
//clrscr();
int number, count = 0;
cout << "ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
cin >> number;
for (int a = 1; a <= number; a++)
{
if (number%a == 0)
{
count++;
}
}
if (count == 2)
{
cout << " PRIME NUMBER \n";
}
else
{
cout << " NOT A PRIME NUMBER \n";
}
//getch();
}
The inclusion of "stdafx.h" is because Visual C++ by default uses pre-compiled headers, and for that to work the first non-comment line in the source file have to be that inclusion.
For the _tmain function, it tells me you have opted to make a WIN32 console project. You can make an empty project, and add files manually as needed, and use only standard C++ features, like having the proper main function instead.

atof truncates fractional parts after QApplication

I created a qt application with qt creator, the code is like,
#include "mainwindow.h"
#include <QDebug>
#include <QApplication>
#include <stdlib.h>
int main(int argc, char *argv[])
{
double before = atof("3.1");
double x;
sscanf("3.1", "%lf", &x);
QApplication a(argc, argv);
double after = atof("3.1");
double y;
sscanf("3.1", "%lf", &y);
MainWindow w;
w.show();
qDebug() << before;
qDebug() << after;
qDebug() << x;
qDebug() << y;
return a.exec();
}
the output is
3.1
3
3.1
3
That means sscanf and atof truncates fractional parts after "QApplication a(argc, argv);". The problem only occurs in Qt5.3 under Linux Mint 17. I tested the same program in windows 8 and Mac OS 10.9, they don't have the same problem. Is it a bug in Linux Qt5.3 or it has something to do with linux c library?
The complete code can be accessed here
See QCoreApplication documentation:
On Unix/Linux Qt is configured to use the system locale settings by
default. This can cause a conflict when using POSIX functions, for
instance, when converting between data types such as floats and
strings, since the notation may differ between locales. To get around
this problem, call the POSIX function setlocale(LC_NUMERIC,"C") right
after initializing QApplication or QCoreApplication to reset the
locale that is used for number formatting to "C"-locale.
I can reproduce your problem and the following code fixes it for me:
QApplication a(argc, argv);
setlocale(LC_NUMERIC,"C");

Cannot find lib file when I've specifed the dll

I'm new to visual c++ and rusty with c++.
I created a dll project following the visual C++ directions. Now I want to test my dll to make sure it's working. I created an empty project and put in tester.cpp. I added the dll to the project references and to the path. Then I try to run it.
Before I included stuff from my dll ("Hello world!") it worked. Now that I've put in my stuff to reference the dll, it fails. The message is:
1>LINK : fatal error LNK1104: cannot open file 'C:\Users\thom\Documents\cworkspace\barnaby\Debug\barnaby.lib'
The thing I don't understand is the reference links to the dll which exists at the path above. Here's my code:
#include <iostream>
#include <string>
#include <vector>
#include "barnaby.h"
int main(int argc, char *argv[]){
std::vector<std::string> tzNames = Barnaby::TimeZoneFunctions::getTimezoneList();
for(std::vector<std::string>::iterator iter = tzNames.begin(); iter != tzNames.end(); iter++){
std::cout << *iter << std::endl;
}
}
ideas?
OK, so I found the answer from http://binglongx.wordpress.com/2009/01/26/visual-c-does-not-generate-lib-file-for-a-dll-project/ which told me to include the following code in my header for the DLL:
#ifdef BARNABY_EXPORTS
#define BARNABY_API __declspec(dllexport)
#else
#define BARNABY_API __declspec(dllimport)
#endif
Then, each function I export you simply precede by:
BARNABY_API int add(){
}
All of this would have been prevented either by click the Export Symbols box on the new project DLL Wizard or by voting yes for lobotomies for application programmers.
Thanks for the help.

Resources