How To Call A String Resource In C++ - string

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

Related

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.

VC.net String to LPWSTR

How can I transform from string to LPWSTR
String^ str= "hello world";
LPWSTR apppath= (LPWSTR)(Marshal::StringToHGlobalAnsi(str).ToPointer());
But it doesn't work.After transformed:
You're trying to read single-byte characters (that's what Marshal::StringToHGlobalAnsi is returning) as wide characters (that's what an LPWSTR type points to), and you're getting whatever is in memory interpreted as a wide-character string.
You need to marshal the appropriate type, and you need to be aware of the lifetime of the result. Here's an example program that shows one way to do it, modified from the MSDN example in marshal_context::marshal_as:
// compile with: /clr
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <msclr\marshal.h>
using namespace System;
using namespace msclr::interop;
int main() {
marshal_context^ context = gcnew marshal_context();
String^ str = "hello world";
LPWSTR apppath = const_cast<wchar_t*>(context->marshal_as<const wchar_t*>(str));
wprintf(L"%s\n", apppath);
delete context;
return 0;
}
Note that a reference to apppath is likely to be bogus after the deletion of context.

Compiling printf() without including <stdio.h>

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.

where do i include the string library in my C++ header file

My simple class won't compile in Visual Studio. It worked before I added the string company member and the getter method getCo() to it. I think I need to put #include the string standard library somewhere but I am not sure where. Any idea where? In my header file, I have:
#pragma once
#ifndef ENGINEER_H_
#define ENGINEER_H_
class engineer {
int years;
string company;
public:
engineer(int years);
~engineer(void);
int getYears();
string getCo();
};
#endif ENGINEER_H_
And in my CPP file for the definition of the class, I have:
#include "StdAfx.h"
#include "engineer.h"
engineer::engineer(int y, string c){
years = y;
company = c;
}
engineer::~engineer(void) {
}
int engineer::getYears() {
return years;
}
string engineer::getCo() {
return company;
}
Put it in the header file, and prefix your usage of string with the namespace std.
Header:
#include <string>
class engineer
{
std::string company;
};
In the implementation file (.cpp) you can prefix the names or have a using directive.
Implementation:
using namespace std; // using directive, no longer need to use std::
Avoid putting the using directive in a header file, as that pollutes the global namespace and can cause problems with naming collisions in other libraries you may wish to use.
Put it in the header file, after the include guards:
#include <string>
using std::string;
This way, it will also be available for your cpp file, and you don't have to include it again.
BTW, the #pragma once and #ifndef ENGINEER_H_ serve the same purpose. You can have only one of them. Code generated by VC use the #pragma, which is shorter and doesn't add a definition, so that's what I'd use (no harm if you leave both, though).

Inputting a string

Can't compile something as simple as this, yet when I change the variable type from
string to int, it compiles and runs fine. Any ideas why this is happening?
#include <iostream>
using namespace std;
void main()
{
string x;
cin>>x;
}
You need:
#include <string>
To get std::string in your application.

Resources