Friends
On HP-UX box when Iam passing a string object to function
Im getting the following below error
Error 422: "../header/Handler.h", line 24 # 'string' is used as a type, but has not been
defined as a type. Perhaps you meant 'String' as in class String
["/opt/aCC/include/SC/String.h", line 66].
int populateBindingHandle(rpc_if_handle_t p_if_spec, string p_cell_name);
why would I get an error to use String.h not
how does a declaration String newstr;
different from
string newstr; ??
Many Thanks
Looks like there is a String class in the header mentioned by the compiler. The compiler thinks you made a typo.
If you want to use STL strings use the following:
#include <string>
int populateBindingHandle(rpc_if_handle_t p_if_spec, std::string ...)
or have a using declaration somewhere:
using std::string;
int populateBindingHandle(rpc_if_handle_t p_if_spec, std::string ...)
Note, the old-style headers have been deprecated, i.e. you should no longer use #include <string.h>
Related
If I set a string as a filename, it doesn't work and I have no idea why. (I'm using codeblocks and it seems to work on other IDEs)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string FileName="Test.txt";
ofstream File;
File.open(FileName);
}
This does not work,while this next one does:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream File;
File.open("Test.txt");
}
Error message:
no matching function for call to std::basic_ofstream::open(std::string&)
Can someone help a bit with this problem, I cannot understand why this error occurs.
Due to what should be considered a historical accident in the early era of C++ standardisation, C++ file streams originally didn't support std::string for filename parameters, only char pointers.
That's why something like File.open(FileName), with FileName being a std::string, didn't work and had to written as File.open(FileName.c_str()).
File.open("Test.txt") always worked because of the usual array conversion rules which allow the "Test.txt" array to be treated like a pointer to its first element.
C++11 fixed the File.open(FileName) problem by adding std::string overloads.
If your compiler doesn't support C++11, then perhaps you should get a newer one. Or perhaps it does support C++11 and you just have to turn on the support with a flag like -std=c++11.
I am confused about the use of #include <string> at the start of a program. For example, in the code below, I don't use #include <string> but the function will still print out the string "Johnny's favorite number is" when it is run.
#include <iostream>
using namespace std;
void printVariable(int number){
cout << "Johnny's favorite number is" << number << endl
}
However, in this code below, it does contain #include <string>.
#include <iostream>
#include <string>
using namespace std;
class Var{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main(){
Var Classy;
Classy.setName("Johnny Bravo");
cout << Classy.getName() << endl;
return 0;
}
Do I only use #include <string> if a variable represents a string?
Do I only use #include <string> if a variable represents a string?
Yes.
Use #include <string> when you use a variable that has type std::string.
The code "text here", contrary to intuition, is not a std::string; it is a string literal, and a C-style string, and a const char[10] convertible to const char*. Welcome to C++ with its legacy oddities.
Your question arises from the fact that you know that something like "aabcd" is a string literal. So, its type should be string. Well, that's not quite true.
C++ has a lot of features from C. Including data types. So, that is a pointer to char (char*), not a string(an instance of the string class). You can create an instance of the string class from a char* (including a string literal) by passing it as argument to the constructor of string. But it is not a string, it's just some misleading terminology.
A similar case is calling things vectors when they are arrays.
If you use the type std::string in your code then you should include the <string> header. There are also a few other types and functions in that header, but std::string is the most commonly used one.
However, you do not need to include this header just to use string literals, which are built into the core language.
In your first case, library "string" is not needed. The object "cout" is supported by library "iostream", thus you have:
#include <iostream>
For the second case, you do explicitly use "string", thus library "string" is required:
#include <string>
I can't declare a string in my program:
string MessageBoxText = CharNameTextBox->Text;
it just doesn't work. It says string is undeclared identifier. What am I missing in the namespace or include or something like that?
Make sure you've included this header:
#include <string>
And then use std::string instead of string. It is because string is defined in std namespace.
And don't write this at namespace scope:
using namespace std; //bad practice if you write this at namespace scope
However, writing it at function scope is not that bad. But the best is one which I suggested before:
Use std::string as:
std::string MessageBoxText = CharNameTextBox->Text;
To use the standard string class in C++ you need to #include <string>. Once you've added the #include directive string will be defined in the std namespace and you can refer to it as std::string.
E.g.
#include <string>
#include <iostream>
int main()
{
std::string hw( "Hello, world!\n" );
std::cout << hw;
return 0;
}
Are you by any way compiling using C++/CLI, the Microsoft extension for .NET, and not standard ISO C++?
In that case you should do the following:
System::String^ MessageBoxText = CharNameTextBox->Text;
Also see the following articles:
How to: Convert Between Various String Types
How to: Convert System::String to Standard String
How to: Convert Standard String to System::String
I am new to c++, trying to debug the following line of code
class cGameError
{
string m_errorText;
public:
cGameError( char *errorText )
{
DP1("***\n*** [ERROR] cGameError thrown! text: [%s]\n***\n",
errorText );
m_errorText = string( errorText );
}
const char *GetText()
{
return m_errorText.c_str();
}
};
enum eResult
{
resAllGood = 0, // function passed with flying colors
resFalse = 1, // function worked and returns 'false'
resFailed = –1, // function failed miserably
resNotImpl = –2, // function has not been implemented
resForceDWord = 0x7FFFFFFF
};
This header file is included in the program as followed
#include "string.h"
#include "stdafx.h"
#include "Chapter 01 MyVersion.h"
#include "cGameError.h"
You need to include <string>, not "string.h". Or in addition to "string.h".
string.h is the C header for the standard C string handling functions (strcpy() and friends.)
<string> is the standard C++ header where 'string' is defined.
You also need to specify the std namespace when using string:
std::string m_errorText;
Or by using:
using namespace std;
Somewhere at the top of your file.
You should also use angle brackets for system include files.
You've provided little enough information that this is only a wild guess, but at first glance, I'd guess the problem is that you haven't included <string>, only "string.h" (the former defines the C++ std::string class, the latter the C functions for manipulating nul-terminated strings.
As an aside, you normally want to use angle-brackets for system headers, so it should be <string.h>.
Try #include <string>, instead of #include "string.h", string.h/cstring is the old C-string header, string is the new C++ std::string class header. And you normally use angle-brackets for system headers.
I am trying to change a certain text box message. It will display my output.
This is what I have in my main()
#include "form2.h"
....
string recvMSG = "random";
182:: Form2::changeOutput(recvMSG);
...
within my form2.h I have:
#include <string.h>
#include <iostream>
#include <stdlib.h>
...
void Form2::changeOutput(string s)
{
QString s1 = i18n(s);
output_box.setText(s1);
}
But i still get:
.ui/form2.h:56: error: ‘string’ has not been declared
Thanks.
Edit:: kk so now its showing::
TCPClient.cpp:182: error: cannot call member function ‘virtual void Form2::changeOutput(std::string)’ without object
string is in the std namespace, so you either need to refer to it as std::string, or you need to make the name available in the current scope with using namespace std; or using std::string;.
Also the header is called string, not string.h, so include it this way:
#include <string>
Generally you also might want to use QT's QString instead of std::string if you are using it in connection with QT components that usually take QString parameters.
I guess you should use the header <string> and then use std::string (even better would be const std::string &)