来自C ++新手的问题。请原谅我,如果它是一个简单的。
我正在尝试运行一个* .cpp代码来读取文本文件并输出格式化文件。但是在运行代码时,我遇到了一些编译错误。我附上了代码和错误。
代码:
#include<iostream>
#include<iomanip>
#include<fstream>
const int cell = 1;
using namespace std;
int main()
{
int id[cell],crop[cell],n;
double root_depth1[cell], root_depth2[cell], root_depth3[cell], root_fract1[cell], root_fract2[cell], root_fract3[cell], x;
double LAI_1[cell],LAI_2[cell],LAI_3[cell],LAI_4[cell],LAI_5[cell],LAI_6[cell];
double LAI_7[cell], LAI_8[cell], LAI_9[cell], LAI_10[cell], LAI_11[cell], LAI_12[cell];
ifstream read;
read.open("veg_par_in.txt");
if(read != 0)
{
for (int i=0;i<cell;i++)
{
read>>id[i]>>crop[i]>>root_depth1[i]>>root_fract1[cell]>>root_depth2[cell]>>root_fract2[cell]>>root_depth3[cell]>>root_fract3[cell];
read>>LAI_1[i]>>LAI_2[i]>>LAI_3[i]>>LAI_4[i]>>LAI_5[i]>>LAI_6[i];
read>>LAI_7[i]>>LAI_8[i]>>LAI_9[i]>>LAI_10[i]>>LAI_11[i]>>LAI_12[i];
}
read.close();
ofstream write;
write.open("veg_par.txt");
for(int j=0;j<cell;j++)
{
write<<id[j]<<" "<<1<<endl;
write<<right<<setw(5)<<crop[j]<<" "<<fixed<<setprecision(6)<<float(1)<<fixed<<setprecision(2)<<" "<<root_depth1[j]<<" "<<root_fract1[cell];
write<<" "<<root_depth2[cell]<<" "<<root_fract2[cell]<<" "<<root_depth3[cell]<<" "<<root_fract3[cell]<<endl;
write<<" "<<fixed<<setprecision(3)<<LAI_1[j]<<" "<<LAI_2[j]<< " "<<LAI_3[j]<<" "<<LAI_4[j]<<" "<<LAI_5[j]<," "<<LAI_6[j];
write<<" "<<LAI_7[j]<<" "<<LAI_8[j]<<" "<<LAI_9[j]<<" "<<LAI_10[j]<<" "<<LAI_11[j]<<" "<<LAI_12[j]<<endl;
}
write.close();
}
else
cout<<"veg_par_in.txt file is missing.";
}
错误消息:
veg_par.cpp: In function ‘int main()’:
veg_par.cpp:17:10: error: no match for ‘operator!=’ (operand types are ‘std::ifstream {aka std::basic_ifstream<char>}’ and ‘int’)
if(read != 0)
veg_par.cpp:17:10: note: candidate: operator!=(int, int) <built-in>
veg_par.cpp:17:10: note: no known conversion for argument 1 from ‘std::ifstream {aka std::basic_ifstream<char>}’ to ‘in ’
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iosfwd:40:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/postypes.h:221:5: note: candidate: template<class _StateT> bool std::
operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/postypes.h:221:5: note: template argument deduction/substitution fa iled:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::fpos<_StateT>’
if(read != 0)
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_algobase.h:64:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_pair.h:448:5: note: candidate: template<class _T1, class _T2> con stexpr bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_pair.h:448:5: note: template argument deduction/substitution fa iled:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::pair<_T1, _T2>’
if(read != 0)In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:304:5: note: candidate: template<class _Iterator> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator!=(const reverse_iterator<_Iterator>& __x,
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:304:5: note: template argument deduction/substitutio n failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::reverse_iterato r<_Iterator>’
if(read != 0)
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:361:5: note: candidate: template<class _IteratorL, cla ss _IteratorR> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator!=(const reverse_iterator<_IteratorL>& __x,
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:361:5: note: template argument deduction/substitutio n failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::reverse_iterato r<_Iterator>’
if(read != 0)
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:1125:5: note: candidate: template<class _IteratorL, cl ass _IteratorR> bool std::operator!=(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)
operator!=(const move_iterator<_IteratorL>& __x,
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:1125:5: note: template argument deduction/substituti on failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::move_iterator<_ IteratorL>’
if(read != 0)
^In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:1131:5: note: candidate: template<class _Iterator> boo l std::operator!=(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)
operator!=(const move_iterator<_Iterator>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:1131:5: note: template argument deduction/substituti on failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::move_iterator<_ IteratorL>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/string:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/allocator.h:158:5: note: candidate: template<class _T1, class _T2> bo ol std::operator!=(const std::allocator<_CharT>&, const std::allocator<_T2>&)
operator!=(const allocator<_T1>&, const allocator<_T2>&)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/allocator.h:158:5: note: template argument deduction/substitution f ailed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::allocator<_Char T>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/string:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/allocator.h:164:5: note: candidate: template<class _Tp> bool std::ope rator!=(const std::allocator<_CharT>&, const std::allocator<_CharT>&)
operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/allocator.h:164:5: note: template argument deduction/substitution f ailed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::allocator<_Char T>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5112:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_Ch arT, _Traits, _Alloc>&)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5112:5: note: template argument deduction/substituti on failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::basic_string<_C harT, _Traits, _Alloc>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5125:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator!=(const _CharT* __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5125:5: note: template argument deduction/substituti on failed:
veg_par.cpp:17:13: note: mismatched types ‘const _CharT*’ and ‘std::basic_ifstream<char>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5137:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5137:5: note: template argument deduction/substituti on failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::basic_string<_C harT, _Traits, _Alloc>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:46:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:311:3: note: candidate: bool std::operator!=(const std::error _code&, const std::error_code&)
operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:311:3: note: no known conversion for argument 1 from ‘std:: ifstream {aka std::basic_ifstream<char>}’ to ‘const std::error_code&’
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:315:3: note: candidate: bool std::operator!=(const std::error _code&, const std::error_condition&)
operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:315:3: note: no known conversion for argument 1 from ‘std:: ifstream {aka std::basic_ifstream<char>}’ to ‘const std::error_code&’
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:319:3: note: candidate: bool std::operator!=(const std::error _condition&, const std::error_code&)
operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:319:3: note: no known conversion for argument 1 from ‘std:: ifstream {aka std::basic_ifstream<char>}’ to ‘const std::error_condition&’
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:323:3: note: candidate: bool std::operator!=(const std::error _condition&, const std::error_condition&)
operator!=(const error_condition& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:323:3: note: no known conversion for argument 1 from ‘std:: ifstream {aka std::basic_ifstream<char>}’ to ‘const std::error_condition&’
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_facets.h:48:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_ios.h:37,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:44,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/streambuf_iterator.h:210:5: note: candidate: template<class _CharT, c lass _Traits> bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT , _Traits>&)
operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/streambuf_iterator.h:210:5: note: template argument deduction/subst itution failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::istreambuf_iter ator<_CharT, _Traits>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/tuple:39:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:37,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_conv.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/locale:43,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iomanip:43,
from veg_par.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/array:246:5: note: candidate: template<class _Tp, long unsigned int _Nm> b ool std::operator!=(const std::array<_Tp, _Nm>&, const std::array<_Tp, _Nm>&)
operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/array:246:5: note: template argument deduction/substitution failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::array<_Tp, _Nm ’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:37:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_conv.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/locale:43,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iomanip:43,
from veg_par.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/tuple:1367:5: note: candidate: template<class ... _TElements, class ... _U Elements> constexpr bool std::operator!=(const std::tuple<_Elements ...>&, const std::tuple<_Elements ...>&)
operator!=(const tuple<_TElements...>& __t,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/tuple:1367:5: note: template argument deduction/substitution failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::tuple<_Elements ...>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_conv.h:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/locale:43,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iomanip:43,
from veg_par.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:667:5: note: candidate: template<class _Tp, class _Dp, c lass _Up, class _Ep> bool std::operator!=(const std::unique_ptr<_Tp, _Dp>&, const std::unique_ptr<_Up, _Ep>&)
operator!=(const unique_ptr<_Tp, _Dp>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:667:5: note: template argument deduction/substitution failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::unique_ptr<_Tp, _Dp>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_conv.h:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/locale:43,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iomanip:43,
from veg_par.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:673:5: note: candidate: template<class _Tp, class _Dp> b ool std::operator!=(const std::unique_ptr<_Tp, _Dp>&, std::nullptr_t)
operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:673:5: note: template argument deduction/substitution failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::unique_ptr<_Tp, _Dp>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_conv.h:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/locale:43,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iomanip:43,
from veg_par.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:678:5: note: candidate: template<class _Tp, class _Dp> b ool std::operator!=(std::nullptr_t, const std::unique_ptr<_Tp, _Dp>&)
operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:678:5: note: template argument deduction/substitution failed:
veg_par.cpp:17:13: note: mismatched types ‘const std::unique_ptr<_Tp, _Dp>’ and ‘int’
if(read != 0)
^
veg_par.cpp:34:109: error: expected primary-expression before ‘,’ token
write<<" "<<fixed<<setprecision(3)<<LAI_1[j]<<" "<<LAI_2[j]<< " "<<LAI_3[j]<<" "<<LAI_4[j]<<" "<<LAI_5[j]<," "<<LAI_6[j ];
^
veg_par.cpp:34:113: error: invalid operands of types ‘const char [2]’ and ‘double’ to binary ‘operator<<’
write<<" "<<fixed<<setprecision(3)<<LAI_1[j]<<" "<<LAI_2[j]<< " "<<LAI_3[j]<<" "<<LAI_4[j]<<" "<<LAI_5[j]<," "<<LAI_6[j ];
~~~^~~~~~~~~ ~
第一个错误表示&#39;运营商没有匹配!=&#39;操作数类型为char和int。我一直在调查这个错误,发现这可能与运算符比较两种类型的操作数类型有关。在这里,&#39;阅读&#39;是一个流变量,它的数据类型为char,运算符正在比较两种数据类型。我很确定运行此代码时的所有错误都来自运算符比较两种数据类型。
我用过&#34; g ++ -g veg_par.cpp -o a.exe&#34;用于编译。
如何解决此错误?任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
第一个错误表示&#39;运营商没有匹配!=&#39;操作数类型为char和int
不完全。您的问题出在此表达式read != 0
中。它正在尝试将std::ifstream
与int
进行比较。由于前者是类类型,因此必须考虑重载运算符以及任何用户定义的转换为int
。没有此类转换,也没有定义operator!=
。所以你不能写这样的比较。
现在,这并不意味着您在if语句的条件下使用read
。它只是意味着您必须以其中一种支持的方式使用它。那些是if(read)
和if(!read)
。 std::ifstream
还有一个operator!
,允许向bool
进行内容转换。