I’m practicing using mulitple files and header files etc. So I have this project which takes two numbers and then adds them. Pretty simple.
Here are my files:
main.cpp
#include <iostream>
#include "add.h"
int main()
{
int x = readNumber();
int y = readNumber();
writeAnswer(x + y);
return(0);
}
io.cpp
int readNumber()
{
int x;
std::cout << "Number: ";
std::cin >> x;
return x;
}
void writeAnswer(int x)
{
std::cout << "Answer: ";
std::cout << x;
}
add.h
#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED
int readNumber();
void writeAnswer(int x);
#endif // #ifndef ADD_H_INCLUDED
The error is showing up in io.cpp. The exact errors are:
Does anyone have any idea why this may be happening? Thanks.
EDIT: I made a small project yesterday with the same amount of files (2 .cpp and 1.h) and I didn’t include the iostream header in the other .cpp and it still compiled and ran fine.
asked Jul 7, 2012 at 14:43
3
add #include <iostream>
to the start of io.cpp
too.
answered Jul 7, 2012 at 14:45
unkulunkuluunkulunkulu
11.4k2 gold badges31 silver badges49 bronze badges
5
If you’re using pre-compiled headers with Microsoft’s compiler (MSVC), remember that it must be:
#include "stdafx.h"
#include <iostream>
and not the other way around:
#include <iostream>
#include "stdafx.h"
In other words, the pre-compiled header include file must always come first. (The compiler should give you an error specifically explaining this if you forget.)
Cody Gray♦
236k50 gold badges486 silver badges567 bronze badges
answered Sep 20, 2018 at 19:41
JukesJukes
3772 silver badges5 bronze badges
0
I had a similar issue and it turned out that i had to add an extra entry in cmake
to include the files.
Since i was also using the zmq library I had to add this to the included libraries as well.
answered May 22, 2018 at 17:26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
// sstream standard header #pragma once #ifndef _SSTREAM_ #define _SSTREAM_ #ifndef RC_INVOKED #include <string> #ifdef _MSC_VER #pragma pack(push,_CRT_PACKING) #pragma warning(push,3) #endif /* _MSC_VER */ _STD_BEGIN #pragma warning(disable:4251) // TEMPLATE CLASS basic_stringbuf template<class _Elem, class _Traits, class _Alloc> class basic_stringbuf : public basic_streambuf<_Elem, _Traits> { // stream buffer maintaining an allocated character array public: typedef _Alloc allocator_type; typedef basic_streambuf<_Elem, _Traits> _Mysb; typedef basic_string<_Elem, _Traits, _Alloc> _Mystr; explicit __CLR_OR_THIS_CALL basic_stringbuf(ios_base::openmode _Mode = ios_base::in | ios_base::out) { // construct empty character buffer from mode _Init(0, 0, _Getstate(_Mode)); } explicit __CLR_OR_THIS_CALL basic_stringbuf(const _Mystr& _Str, ios_base::openmode _Mode = ios_base::in | ios_base::out) { // construct character buffer from string, mode _Init(_Str.c_str(), _Str.size(), _Getstate(_Mode)); } virtual __CLR_OR_THIS_CALL ~basic_stringbuf() { // destroy the object _Tidy(); } enum { // constants for bits in stream state _Allocated = 1, // set if character array storage has been allocated _Constant = 2, // set if character array nonmutable _Noread = 4, // set if character array cannot be read _Append = 8, // set if all writes are appends _Atend = 16}; // set if initial writes are appends typedef int _Strstate; typedef typename _Traits::int_type int_type; typedef typename _Traits::pos_type pos_type; typedef typename _Traits::off_type off_type; _Mystr __CLR_OR_THIS_CALL str() const { // return string copy of character array if (!(_Mystate & _Constant) && _Mysb::pptr() != 0) { // writable, make string from write buffer _Mystr _Str(_Mysb::pbase(), (_Seekhigh < _Mysb::pptr() ? _Mysb::pptr() : _Seekhigh) - _Mysb::pbase()); return (_Str); } else if (!(_Mystate & _Noread) && _Mysb::gptr() != 0) { // readable, make string from read buffer _Mystr _Str(_Mysb::eback(), _Mysb::egptr() - _Mysb::eback()); return (_Str); } else { // inaccessible, return empty string _Mystr _Nul; return (_Nul); } } void __CLR_OR_THIS_CALL str(const _Mystr& _Newstr) { // replace character array from string _Tidy(); _Init(_Newstr.c_str(), _Newstr.size(), _Mystate); } protected: virtual int_type __CLR_OR_THIS_CALL overflow(int_type _Meta = _Traits::eof()) { // put an element to stream if (_Mystate & _Append && _Mysb::pptr() != 0 && _Mysb::pptr() < _Seekhigh) _Mysb::setp(_Mysb::pbase(), _Seekhigh, _Mysb::epptr()); if (_Traits::eq_int_type(_Traits::eof(), _Meta)) return (_Traits::not_eof(_Meta)); // EOF, return success code else if (_Mysb::pptr() != 0 && _Mysb::pptr() < _Mysb::epptr()) { // room in buffer, store it *_Mysb::_Pninc() = _Traits::to_char_type(_Meta); return (_Meta); } else if (_Mystate & _Constant) return (_Traits::eof()); // array nonmutable, fail else { // grow buffer and store element size_t _Oldsize = _Mysb::pptr() == 0 ? 0 : _Mysb::epptr() - _Mysb::eback(); size_t _Newsize = _Oldsize; size_t _Inc = _Newsize / 2 < _MINSIZE ? _MINSIZE : _Newsize / 2; // grow by 50 per cent _Elem *_Newptr = 0; _Elem *_Oldptr = _Mysb::eback(); while (0 < _Inc && INT_MAX - _Inc < _Newsize) _Inc /= 2; // increment causes overflow, halve it if (0 < _Inc) { // finite increment, allocate new character array _Newsize += _Inc; _Newptr = _Al.allocate(_Newsize); } if (0 < _Oldsize) _Traits_helper::copy_s<_Traits>(_Newptr, _Newsize, _Oldptr, _Oldsize); if (_Oldsize == 0) { // first growth, set up pointers _Seekhigh = _Newptr; _Mysb::setp(_Newptr, _Newptr + _Newsize); if (_Mystate & _Noread) _Mysb::setg(_Newptr, 0, _Newptr); else _Mysb::setg(_Newptr, _Newptr, _Newptr + 1); } else { // not first growth, adjust pointers _Seekhigh = _Newptr + (_Seekhigh - _Oldptr); _Mysb::setp(_Newptr + (_Mysb::pbase() - _Oldptr), _Newptr + (_Mysb::pptr() - _Oldptr), _Newptr + _Newsize); if (_Mystate & _Noread) _Mysb::setg(_Newptr, 0, _Newptr); else _Mysb::setg(_Newptr, _Newptr + (_Mysb::gptr() - _Oldptr), _Mysb::pptr() + 1); } if (_Mystate & _Allocated) _Al.deallocate(_Oldptr, _Oldsize); _Mystate |= _Allocated; *_Mysb::_Pninc() = _Traits::to_char_type(_Meta); return (_Meta); } } virtual int_type __CLR_OR_THIS_CALL pbackfail(int_type _Meta = _Traits::eof()) { // put an element back to stream if (_Mysb::gptr() == 0 || _Mysb::gptr() <= _Mysb::eback() || !_Traits::eq_int_type(_Traits::eof(), _Meta) && !_Traits::eq(_Traits::to_char_type(_Meta), _Mysb::gptr()[-1]) && _Mystate & _Constant) return (_Traits::eof()); // can't put back, fail else { // back up one position and store put-back character _Mysb::gbump(-1); if (!_Traits::eq_int_type(_Traits::eof(), _Meta)) *_Mysb::gptr() = _Traits::to_char_type(_Meta); return (_Traits::not_eof(_Meta)); } } virtual int_type __CLR_OR_THIS_CALL underflow() { // get an element from stream, but don't point past it if (_Mysb::gptr() == 0) return (_Traits::eof()); // no character buffer, fail else if (_Mysb::gptr() < _Mysb::egptr()) return (_Traits::to_int_type(*_Mysb::gptr())); // return buffered else if (_Mystate & _Noread || _Mysb::pptr() == 0 || _Mysb::pptr() <= _Mysb::gptr() && _Seekhigh <= _Mysb::gptr()) return (_Traits::eof()); // can't read, fail else { // extend read buffer into written area, then return buffered if (_Seekhigh < _Mysb::pptr()) _Seekhigh = _Mysb::pptr(); _Mysb::setg(_Mysb::eback(), _Mysb::gptr(), _Seekhigh); return (_Traits::to_int_type(*_Mysb::gptr())); } } virtual pos_type __CLR_OR_THIS_CALL seekoff(off_type _Off, ios_base::seekdir _Way, ios_base::openmode _Which = ios_base::in | ios_base::out) { // change position by _Off, according to _Way, _Mode if (_Mysb::pptr() != 0 && _Seekhigh < _Mysb::pptr()) _Seekhigh = _Mysb::pptr(); // update high-water pointer if (_Which & ios_base::in && _Mysb::gptr() != 0) { // position within read buffer if (_Way == ios_base::end) _Off += (off_type)(_Seekhigh - _Mysb::eback()); else if (_Way == ios_base::cur && (_Which & ios_base::out) == 0) _Off += (off_type)(_Mysb::gptr() - _Mysb::eback()); else if (_Way != ios_base::beg) _Off = _BADOFF; if (0 <= _Off && _Off <= _Seekhigh - _Mysb::eback()) { // change read position _Mysb::gbump((int)(_Mysb::eback() - _Mysb::gptr() + _Off)); if (_Which & ios_base::out && _Mysb::pptr() != 0) _Mysb::setp(_Mysb::pbase(), _Mysb::gptr(), _Mysb::epptr()); // change write position to match } else _Off = _BADOFF; } else if (_Which & ios_base::out && _Mysb::pptr() != 0) { // position within write buffer if (_Way == ios_base::end) _Off += (off_type)(_Seekhigh - _Mysb::eback()); else if (_Way == ios_base::cur) _Off += (off_type)(_Mysb::pptr() - _Mysb::eback()); else if (_Way != ios_base::beg) _Off = _BADOFF; if (0 <= _Off && _Off <= _Seekhigh - _Mysb::eback()) _Mysb::pbump((int)(_Mysb::eback() - _Mysb::pptr() + _Off)); // change write position else _Off = _BADOFF; } else _Off = _BADOFF; // neither read nor write buffer selected, fail return (pos_type(_Off)); } virtual pos_type __CLR_OR_THIS_CALL seekpos(pos_type _Ptr, ios_base::openmode _Mode = ios_base::in | ios_base::out) { // change position to _Pos, according to _Mode streamoff _Off = (streamoff)_Ptr; if (_Mysb::pptr() != 0 && _Seekhigh < _Mysb::pptr()) _Seekhigh = _Mysb::pptr(); // update high-water pointer if (_Off == _BADOFF) ; else if (_Mode & ios_base::in && _Mysb::gptr() != 0) { // position within read buffer if (0 <= _Off && _Off <= _Seekhigh - _Mysb::eback()) { // change read position _Mysb::gbump((int)(_Mysb::eback() - _Mysb::gptr() + _Off)); if (_Mode & ios_base::out && _Mysb::pptr() != 0) _Mysb::setp(_Mysb::pbase(), _Mysb::gptr(), _Mysb::epptr()); // change write position to match } else _Off = _BADOFF; } else if (_Mode & ios_base::out && _Mysb::pptr() != 0) { // position within write buffer if (0 <= _Off && _Off <= _Seekhigh - _Mysb::eback()) _Mysb::pbump((int)(_Mysb::eback() - _Mysb::pptr() + _Off)); // change write position else _Off = _BADOFF; } else _Off = _BADOFF; return (streampos(_Off)); } void __CLR_OR_THIS_CALL _Init(const _Elem *_Ptr, size_t _Count, _Strstate _State) { // initialize buffer to [_Ptr, _Ptr + _Count), set state _Seekhigh = 0; _Mystate = _State; if (_Count != 0 && (_Mystate & (_Noread | _Constant)) != (_Noread | _Constant)) { // finite buffer that can be read or written, set it up _Elem *_Pnew = _Al.allocate(_Count); _Traits_helper::copy_s<_Traits>(_Pnew, _Count, _Ptr, _Count); _Seekhigh = _Pnew + _Count; if (!(_Mystate & _Noread)) _Mysb::setg(_Pnew, _Pnew, _Pnew + _Count); // setup read buffer if (!(_Mystate & _Constant)) { // setup write buffer, and maybe read buffer _Mysb::setp(_Pnew, (_Mystate & _Atend) ? _Pnew + _Count : _Pnew, _Pnew + _Count); if (_Mysb::gptr() == 0) _Mysb::setg(_Pnew, 0, _Pnew); } _Mystate |= _Allocated; } } void __CLR_OR_THIS_CALL _Tidy() { // discard any allocated buffer and clear pointers if (_Mystate & _Allocated) _Al.deallocate(_Mysb::eback(), (_Mysb::pptr() != 0 ? _Mysb::epptr() : _Mysb::egptr()) - _Mysb::eback()); _Mysb::setg(0, 0, 0); _Mysb::setp(0, 0); _Seekhigh = 0; _Mystate &= ~_Allocated; } private: enum { // constant for minimum buffer size _MINSIZE = 32}; _Strstate __CLR_OR_THIS_CALL _Getstate(ios_base::openmode _Mode) { // convert open mode to stream state bits _Strstate _State = (_Strstate)0; if (!(_Mode & ios_base::in)) _State |= _Noread; if (!(_Mode & ios_base::out)) _State |= _Constant; if (_Mode & ios_base::app) _State |= _Append; if (_Mode & ios_base::ate) _State |= _Atend; return (_State); } _Elem *_Seekhigh; // the high-water pointer in character array _Strstate _Mystate; // the stream state allocator_type _Al; // the allocator object }; #if defined(_DLL_CPPLIB) && !defined(_M_CEE_PURE) template class _CRTIMP2_PURE basic_stringbuf<char, char_traits<char>, allocator<char> >; template class _CRTIMP2_PURE basic_stringbuf<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >; #endif /* _DLL_CPPLIB */ // TEMPLATE CLASS basic_istringstream template<class _Elem, class _Traits, class _Alloc> class basic_istringstream : public basic_istream<_Elem, _Traits> { // input stream associated with a character array public: typedef _Alloc allocator_type; typedef basic_stringbuf<_Elem, _Traits, _Alloc> _Mysb; typedef basic_string<_Elem, _Traits, _Alloc> _Mystr; explicit __CLR_OR_THIS_CALL basic_istringstream(ios_base::openmode _Mode = ios_base::in) : basic_istream<_Elem, _Traits>(&_Stringbuffer), _Stringbuffer(_Mode | ios_base::in) { // construct empty readable character buffer } explicit __CLR_OR_THIS_CALL basic_istringstream(const _Mystr& _Str, ios_base::openmode _Mode = ios_base::in) : basic_istream<_Elem, _Traits>(&_Stringbuffer), _Stringbuffer(_Str, _Mode | ios_base::in) { // construct readable character buffer from NTCS } virtual __CLR_OR_THIS_CALL ~basic_istringstream() { // destroy the object } _Mysb *__CLR_OR_THIS_CALL rdbuf() const { // return pointer to file buffer return ((_Mysb *)&_Stringbuffer); } _Mystr __CLR_OR_THIS_CALL str() const { // return string copy of character array return (_Stringbuffer.str()); } void __CLR_OR_THIS_CALL str(const _Mystr& _Newstr) { // replace character array from string _Stringbuffer.str(_Newstr); } private: _Mysb _Stringbuffer; // the string buffer }; #if defined(_DLL_CPPLIB) && !defined(_M_CEE_PURE) template class _CRTIMP2_PURE basic_istringstream<char, char_traits<char>, allocator<char> >; template class _CRTIMP2_PURE basic_istringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >; #endif /* _DLL_CPPLIB */ // TEMPLATE CLASS basic_ostringstream template<class _Elem, class _Traits, class _Alloc> class basic_ostringstream : public basic_ostream<_Elem, _Traits> { // output stream associated with a character array public: typedef _Alloc allocator_type; typedef basic_stringbuf<_Elem, _Traits, _Alloc> _Mysb; typedef basic_string<_Elem, _Traits, _Alloc> _Mystr; explicit __CLR_OR_THIS_CALL basic_ostringstream(ios_base::openmode _Mode = ios_base::out) : basic_ostream<_Elem, _Traits>(&_Stringbuffer), _Stringbuffer(_Mode | ios_base::out) { // construct empty writable character buffer } explicit __CLR_OR_THIS_CALL basic_ostringstream(const _Mystr& _Str, ios_base::openmode _Mode = ios_base::out) : basic_ostream<_Elem, _Traits>(&_Stringbuffer), _Stringbuffer(_Str, _Mode | ios_base::out) { // construct writable character buffer from NTCS } virtual __CLR_OR_THIS_CALL ~basic_ostringstream() { // destroy the object } _Mysb *__CLR_OR_THIS_CALL rdbuf() const { // return pointer to buffer return ((_Mysb *)&_Stringbuffer); } _Mystr __CLR_OR_THIS_CALL str() const { // return string copy of character array return (_Stringbuffer.str()); } void __CLR_OR_THIS_CALL str(const _Mystr& _Newstr) { // replace character array from string _Stringbuffer.str(_Newstr); } private: _Mysb _Stringbuffer; // the string buffer }; #if defined(_DLL_CPPLIB) && !defined(_M_CEE_PURE) template class _CRTIMP2_PURE basic_ostringstream<char, char_traits<char>, allocator<char> >; template class _CRTIMP2_PURE basic_ostringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >; #endif /* _DLL_CPPLIB */ // TEMPLATE CLASS basic_stringstream template<class _Elem, class _Traits, class _Alloc> class basic_stringstream : public basic_iostream<_Elem, _Traits> { // input/output stream associated with a character array public: typedef _Elem char_type; typedef _Traits traits_type; typedef _Alloc allocator_type; typedef typename _Traits::int_type int_type; typedef typename _Traits::pos_type pos_type; typedef typename _Traits::off_type off_type; typedef basic_string<_Elem, _Traits, _Alloc> _Mystr; explicit __CLR_OR_THIS_CALL basic_stringstream(ios_base::openmode _Mode = ios_base::in | ios_base::out) : basic_iostream<_Elem, _Traits>(&_Stringbuffer), _Stringbuffer(_Mode) { // construct empty character buffer } explicit __CLR_OR_THIS_CALL basic_stringstream(const _Mystr& _Str, ios_base::openmode _Mode = ios_base::in | ios_base::out) : basic_iostream<_Elem, _Traits>(&_Stringbuffer), _Stringbuffer(_Str, _Mode) { // construct character buffer from NTCS } virtual __CLR_OR_THIS_CALL ~basic_stringstream() { // destroy the object } basic_stringbuf<_Elem, _Traits, _Alloc> *__CLR_OR_THIS_CALL rdbuf() const { // return pointer to buffer return ((basic_stringbuf<_Elem, _Traits, _Alloc> *)&_Stringbuffer); } _Mystr __CLR_OR_THIS_CALL str() const { // return string copy of character array return (_Stringbuffer.str()); } void __CLR_OR_THIS_CALL str(const _Mystr& _Newstr) { // replace character array from string _Stringbuffer.str(_Newstr); } private: basic_stringbuf<_Elem, _Traits, _Alloc> _Stringbuffer; // the string buffer }; #if defined(_DLL_CPPLIB) && !defined(_M_CEE_PURE) template class _CRTIMP2_PURE basic_stringstream<char, char_traits<char>, allocator<char> >; template class _CRTIMP2_PURE basic_stringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >; #endif /* _DLL_CPPLIB */ _STD_END #ifdef _MSC_VER #pragma warning(pop) #pragma pack(pop) #endif /* _MSC_VER */ #endif /* RC_INVOKED */ #endif /* _SSTREAM_ */ |
Get this book -> Problems on Array: For Interviews and Competitive Programming
In this article, we have explored the reason behind the error «cout is not a member of std» and 2 fixes to resolve this compilation error with C++ code.
Table of contents:
- Reason for the error
- Fix 1: Add header file
- Fix 2: For Microsoft’s compiler MSVC
Reason for the error
While compiling a C++ code, you may face the following error:
cout is not a member of std
The reason is that the relevant header files are not provided in the code due to which the compiler is unable to locate the function cout.
Following C++ code which give this error when compiled:
int main() {
std::cout << "data" << std::endl;
return 0;
}
In this article at OpenGenus, we have presented fixes which will resolve this issue.
The cout function is available in iostream header file which needs to be included at the beginning of the C++ code.
#include <iostream>
Following is the working code:
#include <iostream>
int main() {
std::cout << "data" << std::endl;
return 0;
}
Fix 2: For Microsoft’s compiler MSVC
If you are compiling C++ code on Windows using Microsoft’s compiler MSVC (Microsoft Visual Code), include the header file stdafx.h just before iostream.
stdafx.h is a precompiled header file which is used in Microsoft Visual Studio to keep track of files which have been compiled once and not changed following it. This is an optimization in MSVC to reduce compilation time. If it is not added, correct header files are not included in subsequent runs. If iostream header file is not included due to this issue, the compilation error will come.
Include these header files in the same order:
#include "stdafx.h"
#include <iostream>
Following is the working code:
#include "stdafx.h"
#include <iostream>
int main() {
std::cout << "data" << std::endl;
return 0;
}
Compile using MSVC.
With these fixes in this article at OpenGenus, the error must be resolved. Continue with your development.
- Remove From My Forums
-
Question
-
#include<iostream>
#include «stdafx.h»
#include<string>
using namespace std;
#include<io.h>void search(string path,int layer)
// path stands for the folder name; layer stands for the current level of file systems
{
struct _finddata_t filefind;
string curr = path+»\*.*»;
int done = 0, i, handle;if((handle=_findfirst(curr.c_str(),&filefind))==-1){
return; // no file exist in the folder
}while(!(done=_findnext(handle,&filefind)))
{
if(!strcmp(filefind.name,»..»))
continue;for(i=0;i<layer;i++) cout<<» «;
if ((_A_SUBDIR==filefind.attrib)) // if it is a fodler
{
std::cout<<filefind.name<<«(dir)»<<endl;
curr=path+»\»+filefind.name;
search(curr,layer+1);
}
else
{
cout<<filefind.name<<endl;
}
}
_findclose(handle);
}
void main()
{
string path;
cout<<«input folder name»<<endl;
cin>>path;
search(path,0);
}
// that’s the source code>—— Build started: Project: Gary2, Configuration: Debug Win32 ——
1>Compiling…
1>directory.cpp
1>c:usersgarydocumentsvisual studio 2005projectsgary2gary2directory.cpp(17) : warning C4244: ‘=’ : conversion from ‘intptr_t’ to ‘int’, possible loss of data
1>c:usersgarydocumentsvisual studio 2005projectsgary2gary2directory.cpp(26) : error C2065: ‘cout’ : undeclared identifier
1>c:usersgarydocumentsvisual studio 2005projectsgary2gary2directory.cpp(30) : error C2039: ‘cout’ : is not a member of ‘std’
1>c:usersgarydocumentsvisual studio 2005projectsgary2gary2directory.cpp(45) : error C2065: ‘cin’ : undeclared identifier
Answers
-
Thank you, I have already found the solution. The fatal error i made here is putting stdfax in the wrong position. I should put it in the front of iostream
-
Marked as answer by
Thursday, December 17, 2009 7:13 AM
-
Marked as answer by
Hi,
Sorry, I hope this error is something real this time. I’m using GCC 5.4 and qt 5.6.2. I’m running from a GNU Guix build environment (technically on top Ubuntu, but the build is run in chroot so that isn’t relevant).
It fails like this:
g++ -c -pipe -O3 -std=gnu++0x -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG_OUTPUT -DQT_NO_DEBUG -DQT_SQL_LIB -DQT_CORE_LIB -I../../src/CnvHunter -I. -I../../src/cppCORE -I../../src/cppXML -I../../src/cppNGS -I../../bamtools/include -I../../src/cppNGSD -I/gnu/store/6fgh94phyq4239grgk8z7yjggldrvwbb-qt-5.6.2/include -I/gnu/store/6fgh94phyq4239grgk8z7yjggldrvwbb-qt-5.6.2/include/QtSql -I/gnu/store/6fgh94phyq4239grgk8z7yjggldrvwbb-qt-5.6.2/include/QtCore -I. -I/gnu/store/6fgh94phyq4239grgk8z7yjggldrvwbb-qt-5.6.2/mkspecs/linux-g++ -o main.o ../../src/CnvHunter/main.cpp
../../src/CnvHunter/main.cpp: In member function ‘virtual void ConcreteTool::main()’:
../../src/CnvHunter/main.cpp:1068:15: error: ‘pow’ is not a member of ‘std’
rmsd += std::pow(samples[j]->doc[e]-samples[i]->doc[e], 2);
^
../../src/CnvHunter/main.cpp:1068:15: note: suggested alternative:
In file included from /gnu/store/n6nvxlk2j8ysffjh3jphn1k5silnakh6-glibc-2.25/include/features.h:410:0,
from /gnu/store/5sv5zy2kgg6iaqyv8zw49w4243j0xkd0-gcc-5.4.0/include/c++/x86_64-unknown-linux-gnu/bits/os_defines.h:39,
from /gnu/store/5sv5zy2kgg6iaqyv8zw49w4243j0xkd0-gcc-5.4.0/include/c++/x86_64-unknown-linux-gnu/bits/c++config.h:482,
from /gnu/store/5sv5zy2kgg6iaqyv8zw49w4243j0xkd0-gcc-5.4.0/include/c++/cstddef:44,
from /gnu/store/6fgh94phyq4239grgk8z7yjggldrvwbb-qt-5.6.2/include/QtCore/qglobal.h:39,
from ../../src/cppCORE/cppCORE_global.h:4,
from ../../src/cppCORE/ToolBase.h:4,
from ../../src/CnvHunter/main.cpp:1:
/gnu/store/n6nvxlk2j8ysffjh3jphn1k5silnakh6-glibc-2.25/include/bits/mathcalls.h:155:1: note: ‘pow’
__MATHCALL_VEC (pow,, (_Mdouble_ __x, _Mdouble_ __y));
^
../../src/CnvHunter/main.cpp:1070:19: error: ‘sqrt’ is not a member of ‘std’
rmsd = 1.0 / std::sqrt(rmsd/exon_indices.count());
^
../../src/CnvHunter/main.cpp:1070:19: note: suggested alternative:
In file included from /gnu/store/n6nvxlk2j8ysffjh3jphn1k5silnakh6-glibc-2.25/include/features.h:410:0,
from /gnu/store/5sv5zy2kgg6iaqyv8zw49w4243j0xkd0-gcc-5.4.0/include/c++/x86_64-unknown-linux-gnu/bits/os_defines.h:39,
from /gnu/store/5sv5zy2kgg6iaqyv8zw49w4243j0xkd0-gcc-5.4.0/include/c++/x86_64-unknown-linux-gnu/bits/c++config.h:482,
from /gnu/store/5sv5zy2kgg6iaqyv8zw49w4243j0xkd0-gcc-5.4.0/include/c++/cstddef:44,
from /gnu/store/6fgh94phyq4239grgk8z7yjggldrvwbb-qt-5.6.2/include/QtCore/qglobal.h:39,
from ../../src/cppCORE/cppCORE_global.h:4,
from ../../src/cppCORE/ToolBase.h:4,
from ../../src/CnvHunter/main.cpp:1:
/gnu/store/n6nvxlk2j8ysffjh3jphn1k5silnakh6-glibc-2.25/include/bits/mathcalls.h:158:1: note: ‘sqrt’
__MATHCALL (sqrt,, (_Mdouble_ __x));
^
make[2]: *** [Makefile:773: main.o] Error 1
make[2]: Leaving directory '/tmp/guix-build-ngs-bits-0-1.c39f1807.drv-0/source/build-tools-Linux-Release/CnvHunter'
make[1]: *** [Makefile:1287: sub-CnvHunter-make_first] Error 2
make[1]: Leaving directory '/tmp/guix-build-ngs-bits-0-1.c39f1807.drv-0/source/build-tools-Linux-Release'
make: *** [Makefile:46: build_tools_release] Error 2
I put the full build log at https://gist.github.com/wwood/3118edc1036124628da6d4d1950c5ccb
I was able to get compilation to succeed by applying this patch:
diff --git a/src/CnvHunter/main.cpp b/src/CnvHunter/main.cpp
index e2edcf8..c4317ca 100644
--- a/src/CnvHunter/main.cpp
+++ b/src/CnvHunter/main.cpp
@@ -11,6 +11,7 @@
#include <QVector>
#include <QFileInfo>
#include <QDir>
+#include <cmath>
#include "math.h"
class SampleCorrelation;
Perhaps it makes sense to apply that or something similar.
- Forum
- Beginners
- not a member of std
not a member of std
This problem has caused me hours of getting nothing done over the last 2 days…
If i make my own class and include a library in the .cpp, I keep getting ONE of these errors:
‘vector’ not a member of std;
‘string’ not a member of std;
in my .cpp file i have #include <vector> and #include <string>
i do NOT use «using namespace std;»
std::string x;
is now working, when it would not work for my earlier class.
and yet
std::vector<int> my_vector;
gives me the vector error.
I can’t figure out where it is going wrong. Sometimes it throws one error, and at times it throws the other. The code for the 2 classes is identical.
This is all very confusing… so here is the code that is throwing the vector error and NOT the string error.
|
|
|
|
Last edited on
Include <string> and <vector> inside the header file.
|
|
std::mystring x;
is now working, when it would not work for my earlier class.
You added your own class to the
std
namespace? Don’t do that.
I’m sorry that was a typo!! I should have just copy and pasted my code.
std::string x;
Thank you for the help. I’m going to try it out now!
———
Wow… I thought you only had to include it in the .cpp!
I need to learn more about what happens when you make a class!
Thank you guys for saving me hours of time!
Last edited on
I need to learn more about what happens when you make a class!
You need to learn more about what happens when you include a file. #include is just an automatic copy paste. It simply inserts the content of the file as-is. Instead of including a file you can copy the content of the file and paste it where you would have placed the include. That would give the exact same result.
Sometimes it can look like you don’t need to include some headers in a header file because they are included by the source file. If you have no includes inside Player.h you could still make it work by including the headers that Player.h needs before including Player.h.
|
|
But I don’t recommend doing it this way. It’s much better if you always try to make sure that the header files can be compiled on their own without relying on the file that includes them to work.
Topic archived. No new replies allowed.
- Forum
- The Ubuntu Forum Community
- Ubuntu Specialised Support
- Development & Programming
- Programming Talk
- Cout not a member of STD class? WTH?
-
Cout not a member of STD class? WTH?
I’m trying to learn socket programming in C++ using a tutorial online. After giving up, I just copied every file from the website and ran make as instructed. It says cout is not a member of std… but cout is ALWAYS a member of std (as far as I know) so it’s very frustrating.
g++ -c -o ServerSocket.o ServerSocket.cpp
g++ -c -o Socket.o Socket.cpp
Socket.cpp: In member function �int Socket::recv(std::string&) const�:
Socket.cpp:135:7: error: �cout� is not a member of �std�
make: *** [Socket.o] Error 1If something is not broken, fix it! Modfy every bit of it you can! Break it! This is how improvement and real education happens.
-
Re: Cout not a member of STD class? WTH?
I don’t do C++, but I’m 99% sure you’re missing:
Code:
#include <iostream>
-
Re: Cout not a member of STD class? WTH?
What is the actual code? You are making us guess over here. You need to call cout with:
Code:
std::cout << " Hello Cruel World " << std::endl;
-
Re: Cout not a member of STD class? WTH?
Yes, I have #include <iostream> in my code
Neither this code:Code:
#include <iostream> using namespace std; int main() { cout << "test" << endl; return 0; }
not this code:
Code:
#include <iostream> int main() { std::cout << "test" << endl; return 0; }
Seems to work . . .
Although, from that I get a slightly different error:
Code:
/tmp/cc4SNKht.o: In function `main': test.cpp:(.text+0x14): undefined reference to `std::cout' test.cpp:(.text+0x19): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' test.cpp:(.text+0x21): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' test.cpp:(.text+0x29): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))' /tmp/cc4SNKht.o: In function `__static_initialization_and_destruction_0(int, int)': test.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::Init()' test.cpp:(.text+0x56): undefined reference to `std::ios_base::Init::~Init()' collect2: ld returned 1 exit status
If something is not broken, fix it! Modfy every bit of it you can! Break it! This is how improvement and real education happens.
-
Re: Cout not a member of STD class? WTH?
WOW, I was using gcc, not g++! (well, the second time I was) Sorry for wasting your time everyone! lol
If something is not broken, fix it! Modfy every bit of it you can! Break it! This is how improvement and real education happens.