user2557850
user2557850

Reputation: 109

C++ -'Stream' Undeclared identifier

Newbie here basically I want to load a file to the input stream. I get the following error

error C2065: 'Stream' : undeclared identifier.

#pragma once
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>

namespace test2 {

  using namespace System;
  using namespace System::ComponentModel;
  using namespace System::Collections;
  using namespace System::Windows::Forms;
  using namespace System::Data;
  using namespace System::Drawing;


  public ref class Form1 : public System::Windows::Forms::Form
  {

    // ...

    private: System::Void browse_Click(System::Object^  sender, System::EventArgs^  e) {
        Stream^ myStream;
        OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

        openFileDialog1->InitialDirectory = "c:\\";
        openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1->FilterIndex = 2;
        openFileDialog1->RestoreDirectory = true;

        if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
        {
            if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
            {
                //code
                myStream->Close();
            }
        }
    }

  };
} 

Upvotes: 1

Views: 5463

Answers (2)

Cody Gray
Cody Gray

Reputation: 244782

The .NET Stream class is defined in the System.IO namespace, so you'll either need to…

  • qualify the name of the type in the object declaration and all subsequent uses

    IO::Stream^ myStream;
    
  • or add a using directive to the top of your code file

    using namespace System::IO;
    

Upvotes: 3

Niko
Niko

Reputation: 26730

You simply forgot to specify the correct namespace for the Stream class:

System::IO::Stream^ myStream;

Upvotes: 0

Related Questions