P: 1
when I compile it :
hxjnsq@hxjnsq-VirtualBox:~$ g++ stat.cc
stat.cc:19:11: error: ‘ostream’ does not name a type
stat.cc:49:1: error: ‘ostream’ does not name a type
stat.cc: In function ‘int main(int, char**)’:
stat.cc:100:1: error: ‘cerr’ was not declared in this scope
stat.cc:104:1: error: ‘cout’ was not declared in this scope
I don't know why?
  1. //stat.cc
  2. #include<iostream>
  3. #include<string.h>
  4. #include<errno.h>
  5. #include<sys/types.h>
  6. #include<sys/stat.h>
  7. #include<ostream>
  8. class Stat : public stat
  9. {
  10. private:
  11. char *path;
  12. int fd;
  13. public:
  14. Stat(){path=0;fd=-1;}
  15. ~Stat();
  16. Stat & examine(const char *pathname);
  17. Stat & examine(int fd);
  18. int operator(Stat &o);
  19. friend ostream & operator<<(ostream &out , Stat &o);
  20. };
  21. Stat::~Stat()
  22. {
  23. if(path)
  24. delete path;
  25. }
  26. Stat & Stat::examine(const char *pathname)
  27. {
  28. if(path)
  29. delete path;
  30. path=strdup(pathname);
  31. fd=-1;
  32. if(::stat(path,this)-1)
  33. throw errno;
  34. return *this;
  35. }
  36. Stat & Stat::examine(int fd)
  37. {
  38. if(path)
  39. {
  40. delete path;
  41. path=0;
  42. }
  43. this->fd=fd;
  44. if(::fstat(fd,this)-1)
  45. throw errno;
  46. return *this;
  47. }
  48. ostream & operator<<(ostream &out , Stat &o)
  49. {
  50. if(o.fd-1&&!o.path)
  51. {
  52. out<<'No current infomation.'
  53. return out;
  54. }
  55. if(o.path)
  56. cout<<'stat('<<o.path<<'){n';
  57. else
  58. cout<<'fstat('<<o.fd<<'){n';
  59. cout<<'tst_dev=t'<<o.st_dev<<';n'
  60. <<'tst_ino=t'<<o.st_ino<<';n';
  61. cout.setf(ios::oct,ios::basefield);
  62. cout<<'tst_mode=t'<<'0'<<o.st_mode<<';n';
  63. cout.setf(ios::dec,ios::basefield);
  64. cout<<'tst_nlink=t'<<o.st_nlink<<';n'
  65. <<'tst_uid=t'<<o.st_uid<<';n'
  66. <<'tst_gid=t'<<o.st_gid<<';n'
  67. <<'tst_rdev=t'<<o.st_rdev<<';n'
  68. <<'tst_atime=t'<<o.st_atime<<';n';
  69. return out;
  70. }
  71. int Stat::operator(Stat &o)
  72. {
  73. if(fd-1&&!path)
  74. throw EINVAL;
  75. if(o.fd-1 && !o.path)
  76. throw EINVAL;
  77. if(o.st_dev!=st_dev o.st_ino!=st_ino)
  78. return 0;
  79. return 1;
  80. }
  81. int main(int argc,char **argv)
  82. {
  83. int x;
  84. Stat t;
  85. Stat s;
  86. t.examine('./stat');
  87. for(x=1;x<argc;++x)
  88. {
  89. try{
  90. s.examine(argv[x]);
  91. }catch(int e)
  92. {
  93. cerr<<strerror(e)<<':stat(2) of'
  94. <<argv[x]<<'n';
  95. continue;
  96. }
  97. cout<<s<<'n';
  98. cout<<''<<argv[x]<<' is'
  99. <<(st?'same':'not the same')
  100. <<'file as ./statn';
  101. }
  102. return 0;
  103. }
  1. Dev C++ Cout Does Not Name A Type 2
  2. C++ Class Does Not Name A Type
Not

Aug 31, 2011  Need help? Post your question and get tips & solutions from a community of 449,590 IT Pros & Developers. It's quick & easy. 求助dev c cin does not name a type 怎么办? 我来答 新人答题领红包. /fallout-tactics-c-dev-pheonix.html. Offt is a POSIX type, but not an ISO-C (ANSI) type. If you compile with -ansi, (or any of the other standards selection options which asserts STRICTANSI checking), you cannot expect it to be defined by the compiler implementation; it is a non-standard option with user-namespace semantics, so you need to define it. Off64t is similar, but currently not even a POSIX standard type, AFAIK.

  • C++ Basics
  • C++ Object Oriented

Allegro.cc Forums » Programming Questions » 'does not name a type' error, general c question Credits go to James Stanley and Milan Mimica for helping out! This thread is locked; no one can reply to it.

Dev C++ Cout Does Not Name A Type 2

  • C++ Advanced
  • C++ Useful Resources
  • Selected Reading

A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive −

C++ Class Does Not Name A Type

There are following basic types of variable in C++ as explained in last chapter −

Sr.NoType & Description
1

bool

Stores either value true or false.

2

char

Typically a single octet (one byte). This is an integer type.

3

int

The most natural size of integer for the machine.

4

float

A single-precision floating point value.

5

double

A double-precision floating point value.

6

void

Represents the absence of type.

7

wchar_t

A wide character type.

C++ also allows to define various other types of variables, which we will cover in subsequent chapters like Enumeration, Pointer, Array, Reference, Data structures, and Classes.

Following section will cover how to define, declare and use various types of variables.

Variable Definition in C++

A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type, and contains a list of one or more variables of that type as follows −

Here, type must be a valid C++ data type including char, w_char, int, float, double, bool or any user-defined object, etc., and variable_list may consist of one or more identifier names separated by commas. Some valid declarations are shown here −

The line int i, j, k; both declares and defines the variables i, j and k; which instructs the compiler to create variables named i, j and k of type int.

Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows −

Some examples are −

For definition without an initializer: variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables is undefined.

Variable Declaration in C++

A variable declaration provides assurance to the compiler that there is one variable existing with the given type and name so that compiler proceed for further compilation without needing complete detail about the variable. A variable declaration has its meaning at the time of compilation only, compiler needs actual variable definition at the time of linking of the program.

A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program. You will use extern keyword to declare a variable at any place. Though you can declare a variable multiple times in your C++ program, but it can be defined only once in a file, a function or a block of code.

Example

Try the following example where a variable has been declared at the top, but it has been defined inside the main function −

When the above code is compiled and executed, it produces the following result −

Same concept applies on function declaration where you provide a function name at the time of its declaration and its actual definition can be given anywhere else. For example −

Lvalues and Rvalues

There are two kinds of expressions in C++ −

  • lvalue − Expressions that refer to a memory location is called 'lvalue' expression. An lvalue may appear as either the left-hand or right-hand side of an assignment.

  • rvalue − The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right- but not left-hand side of an assignment.

Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side. Following is a valid statement −

But the following is not a valid statement and would generate compile-time error −