Compiling printf() without including <stdio.h> - visual-c++

How msvc11 can compile printf("msvc"); with only <iostream> header?
#include <iostream>
using namespace std;
int main()
{
printf("test123");
cin.get();
return EXIT_SUCCESS;
}
No error...
msvc11

For Visual Studio 2013, iostream includes istream which includes ostream which includes ios which includes xlocnum which includes cstdio which includes stdio.h. It's just standard header file spill-over.

Related

Visual C++ compiler bug?

I've reduced my case as much as possible below.
#include <vector>
#include <atomic>
#include <chrono>
using namespace std;
class Unused
{
private:
vector<vector<unsigned>> data;
atomic<unsigned> counter;
};
class Timer
{
private:
chrono::time_point<chrono::high_resolution_clock> begin;
public:
void start()
{
begin = std::chrono::high_resolution_clock::now();
}
};
int main()
{
Unused unused;
vector<Timer> timers;
timers.resize(1);
timers[0].start();
}
I've compiled it as (note the specific flags):
cl /O2 /GL /EHsc driver.cpp
This is with
Microsoft (R) C/C++-Optimierungscompiler Version 19.27.29111 für x86
Microsoft (R) Incremental Linker Version 14.27.29111.0
but I've tried a couple of other recent versions as well. The executable segfaults with a memory access violation. It works with g++, and it works if I change the compile flags. It also works is I simplify the code further.
Is this a compiler bug?
It was indeed a compiler bug, https://developercommunity.visualstudio.com/content/problem/1157189/possible-compiler-bug-1.html.

editor able to find not included file c++

My code :
main.cpp
#include <iostream>
namespace file {
#include "file.cpp"
}
namespace file2 {
#include "file1.cpp"
}
int main() {
file::hello();
return 0;
}
file.cpp
#include <iostream>
void hello() {
std::cout << "hello";
}
file1.cpp
#include <iostream>
void hello() {
std::cout << "hello world";
}
My problem:
I use virtual studio, and i don't know why this does not work.I try it allready on CodeBlock and it was fine. But in VS i have error with at least one repeatedly defined symbol has been found.
Sorry for my english.
Since VS still builds and links file.cpp and file1.cpp to the executable you get the errors. You can exclude them from the project by changing its properties or make them regular header files and include them as such.
The include will literally just copy the content of the file into the name space declaration of main.cpp.

msvc 15.3.1 compiler issue

I have found a strange behavior (probably an issue) while trying to compile a simple application with MS VC 15.3.1 (after applying VC 2017 Upgrade 3):
#include <iostream>
#include <algorithm>
#include <string>
// compiles if commenting out the line below
#include <vector>
#include <list>
class A
{
std::string s;
public:
A(const std::string& str) : s(str)
{
}
A(A&& other)
{
// compiles if changing std::swap<std::string>() to std::swap()
std::swap<std::string>(s, other.s);
}
};
int main(int argc, char *argv[])
{
return 0;
}
the compiler emits error :
1>d:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.11.25503\include\vector(2131):
error C2039: '_Alloc': is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
This code compiles without any issues with VC 15.2, VS2015 and VS2013 toolsets.

Compiling codes with *_s in Visual C++ 6.0

I'm trying to compile some old codes in Visual C++ 6.0. The DSW file was missing, so I'm adding all the codes into a new workspace.
I have a.cpp as follows
#include "vld.h"
#include "afx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "b.h"
...
void function_1(char* string1)
{
char buf[2000];
strcpy_s(&buf[0], 2000, string1);
strcat_s(&buf[0], 2000, "_append");
...
}
There are other functions used such as fopen_s, strncpy_s, strncat_s in a.cpp as well.
And b.h is
#include <stdio.h>
#include <string.h>
char function_2(unsigned char * string2, int abc, int def)
{
char buf2[200];
sprintf_s(buf2, 200, "abcdef");
strcat_s(buf2, 200, "ghijkl");
}
Despite already having stdio.h and string.h, I still get the errors
'sprintf_s': undeclared identifier
'strcat_s': undeclared identifier
'strcpy_s': undeclared identifier
'fopen_s': undeclared identifier
'strncpy_s': undeclared identifier
'strncat_s': undeclared identifier
for both a.cpp and b.h.
Is there any missing settings that I've left out? Why am I getting these errors?
The _s version function were added to Visual Studio 2005. So you need use more modern Visual Studio version.

How To Call A String Resource In C++

in resource.h
#define String1 333
in resource.rc
#include <windows.h>
#include "resource.h"
STRINGTABLE
{
STRING1 "hie people"
}
in main.cpp
#include<iostream.h>
#include<resource.h>
#include<windows.h>
using namespace std;
int main{
cout<<here i want to output string value from resource how to call the string;
}
and one more problem i am compiling in code blocks .it says resource.h is not there where i am wrong
I assume that it is Visual C++ and you are using MFC. It is as simple as calling:
::LoadString(...)
And if you are using MFC, then
CString str;
str.LoadString(STRING1)
LoadString from MSDN
An Example here of how to use LoadString

Resources