C ++ iostream / savefiledialog错误

时间:2012-11-28 14:28:00

标签: visual-c++ c++-cli iostream savefiledialog

我正在使用SaveFileDialog class example 要结合使用,请在用户定义的目录中创建一个文本文件。

这是我的代码中存在冲突的部分:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             array<Image^> ^imageArray ;
             int CurrentImage=0;
             imageArray = gcnew array<Image^>(9);
             array<String^>^ names = {L"adc1.bmp" ,"adc2.bmp","adc3.bmp","adc4.bmp","adc5.bmp","adc6.bmp","adc7.bmp","adc8.bmp"};
             for(int i=0; i<8; i++)
             {
                 System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
                 imageArray[i] =(cli::safe_cast<System::Drawing::Bitmap^>(resources->GetObject(names[i])));
             }

             if((this->comboBox4->Text == String::Empty)||(this->textBox2->Text == String::Empty)){
                 this->textBox1->Text="please select port and save file directory";

             }


             else{ // start assigning
                 try{ // first make sure port isn't busy/open
                     if(!this->serialPort1->IsOpen){
                         // select the port whose name is in comboBox4 (select port)
                         this->serialPort1->PortName=this->comboBox4->Text;
                         //open the port
                         this->serialPort1->Open();
                         // sending
                         String^ name_ = this->serialPort1->PortName;
                         String^ START; 
                         int m=0;
                         //send data to setup timer on the microcontroller
                         this->serialPort1->WriteLine(START);
                         Stream^ myStream;
                         SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog;
                         saveFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                         saveFileDialog1->FilterIndex = 2;
                         saveFileDialog1->RestoreDirectory = true;
                         if ( saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK  )
                         {
                             if ( (myStream = saveFileDialog1->OpenFile()) != nullptr )
                             {
                                 // receiving
                                 myStream<<"ADC1;ADC2;ADC3;ADC4;ADC5;ADC6;ADC7;ADC8"<<endl;
                                 while(START){
                                     myStream<<this->serialPort1->ReadLine()<<";";
                                     pictureBox1->image=imagearray[m];
                                     m++;
                                     if(m==7){
                                         myStream<<endl;
                                         m=0;
                                     }
                                 }
                             }}}
                     else{
                         this->textBox1->Text="Warning: port is busy or isn't open";
                     }
                 }
                 catch(UnauthorizedAccessException^){
                     this->textBox1->Text="Unauthorized access";
                 }
             }

         }

但是,我收到以下错误:

    1>------ Build started: Project: Data_Logger, Configuration: Debug Win32 ------
1>  Data_Logger.cpp
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(1085) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &&,_Ty)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &&' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(968) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,unsigned char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(958) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const unsigned char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(951) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,signed char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(944) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const signed char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(937) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,_Elem)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(898) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const _Elem *)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(851) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(811) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(764) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,char)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(726) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const char *)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(679) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(301): error C2676: binary '<<' : 'System::IO::Stream ^' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(1085) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &&,_Ty)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &&' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(968) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,unsigned char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(958) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const unsigned char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(951) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,signed char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(944) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const signed char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(937) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,_Elem)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(898) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const _Elem *)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(851) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(811) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(764) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,char)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(726) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const char *)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(679) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(304): error C2676: binary '<<' : 'System::IO::Stream ^' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(305): error C2039: 'image' : is not a member of 'System::Windows::Forms::PictureBox'
1>          c:\program files\reference assemblies\microsoft\framework\.netframework\v4.0\system.windows.forms.dll : see declaration of 'System::Windows::Forms::PictureBox'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(305): error C2065: 'imagearray' : undeclared identifier
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(1085) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &&,_Ty)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &&' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(968) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,unsigned char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(958) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const unsigned char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(951) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,signed char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(944) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const signed char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(937) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,_Elem)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(898) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const _Elem *)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(851) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(811) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(764) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,char)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(726) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const char *)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'System::IO::Stream ^'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(679) : see declaration of 'std::operator <<'
1>c:\documents and settings\310098394\desktop\programming\visual c++\data_logger\data_logger\Form1.h(308): error C2676: binary '<<' : 'System::IO::Stream ^' does not define this operator or a conversion to a type acceptable to the predefined operator
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

错误从以下行开始:myStream<<"ADC1;ADC2;ADC3;ADC4;ADC5;ADC6;ADC7;ADC8"<<endl;

之前我使用过ofstream并且有效。但是,我希望用户选择目录并命名输出文件,我不确定是否可以使用ofstream。我真的很感激,如果有人能帮我解决与myStream相关的错误。

提前致谢

1 个答案:

答案 0 :(得分:1)

System :: IO :: Stream不支持运算符&lt;&lt;和运算符&gt;&gt;像std :: [i | o | io] stream。要写入System :: IO :: Stream,请使用Stream :: Write方法。

查看System :: IO :: Stream:

的文档

http://msdn.microsoft.com/en-us/library/system.io.stream.aspx