3 Years
Discussion Span
Last Post by basit_3

why this happen help me ddanbe
error C2065: 'string' : undeclared identifier
i also include string library header`

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
            string number;

             MessageBox::Show("Work on it Soon complete");




While I cannot be certain, it is likely due to a lack of scoping on the string declaration. The C++ standard string class resides in the std namespace, and you would need to scope the declaration accordingly:

            std::string number;

Alternately, if you expect to have a large number of string objects, you could bring the string class into scope throughout the file:

using std::string;

It is not recommended that you scope the entire namespace with using namespace std;, as this can lead to namespace conflicts. While the namespace-level scoping can be a convenience for small programs, it quickly becomes problematic, so it is best to avoid it unless you are certain it will not cause conflicts, and using in general should never be used in a header file, as it will cascade into any file that the header is included in.

Since you are using C++/CLI, rather than standard C++, you should also be aware that .Net Framework has its own String class, which is not compatible with the C++ standard string class. Most .Net functions expect a String, and won't work with a string.

Edited by Schol-R-LEA


The String class( remark the capital S) resides in the System namespace, so you don't have to include <string> or use using namespace std;
this will work:

            String^ number;
            number = txtBox->Text;
            MessageBox::Show("Work on it Soon complete");

But as Schol-R-LEA pointed out, I don't think VS C++ is always standard C++.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.