-2

Here is my code, im trying to make my own string class but im stuck here... my teacher is not good :/ well i got so many errors but i want to know why this error appears "invalid preprocesing directive #ifndef_CADENA_H"

#include <iostream>
#include <cstdlib>
#ifndef_CADENA_H
#define_CADENA_H
    class cadena {
  char* Ptrcad;
  int tam;
  
  public :
   
  cadena(void);
  char* get(void);
  void set(*char**)
  int longitud;
  ~ Cadena (void);
 };
 Cadena :: Cadena(){
  Ptrcad = NULL;
  tam = 0;
 }
 char* Cadena :: get(){
  return Ptrcad;
 }
 void Cadena :: set(const char* x){
  while(*(x+tam)!='\0'){
   tam++;
  }
  Ptrcad = new char (tam);
  for(int i=0;i<tam;i++){
   *(Ptrcad +i)= *(x+i);
  }
 } 

int Cadena :: longitud(){
 return tam;
}
 Cadena :: ~ Cadena(){
  delete [] Ptrcad;
  tam =0;
 }
  • 2
    You need a space between the `#ifndef` and the `_CADENA_H` (and similarly with `#define`). You also should avoid names starting with `_`; they are basically reserved for the implementation — which means you might run into problems sometime and it would be your fault, not someone else's fault. – Jonathan Leffler Oct 12 '14 at 04:48
  • 2
    1) Change to `#ifndef _CADENA_H` (make sure there's a space), 2) Also change to `#define _CADENA_H` (same problem), 3) make sure you have an`#endif`, followed by a blank line, at the end of your header file. – FoggyDay Oct 12 '14 at 04:50
  • @FoggyDay: the blank line after the `#endif` isn't necessary. – Jonathan Leffler Oct 12 '14 at 04:56
  • It was necessary in older C compilers, and it's good practice today. – FoggyDay Oct 12 '14 at 04:57

1 Answers1

2

Because of the underscore I believe. It should be #ifndef CADENA_H and #define CADENA_H. Unless you want to keep the underscore in the name (not reccommended in this case), then it should be #ifndef _CADENA_H and #define _CADENA_H

Baher Ramzy
  • 171
  • 6
  • 1
    No, the underscore is a good convention for a "header guard". The problem is that there *MUST BE A SPACE*. – FoggyDay Oct 12 '14 at 04:55
  • @FoggyDay No, it's not. Everything that begins with and underscore and followed by an uppercase letter is a reserved identifier in C++. (Also: everything that has a double underscore in it is reserved as well.) – Csq Oct 12 '14 at 07:45