0
#include <iostream>

using namespace std;

class String
{
    public : 
     String :: String(char* ch);
    public : ~String ();

    private :
               char* text;
               size_t sizeoftext;

    public :  void imprimircadena();
};

String :: String (char* ch)
{
   sizeoftext = strlen(ch);

   //pido la cantidad de memoria correcta para el string
   text = new char[sizeoftext];


    for (int i=0;i<=sizeoftext-1;i++)
    {
        text[i] = ch[i];
    }

}

String :: ~String()
{
    delete[] text;

};

void :: String imprimircadena() //problem is here
{
      for (int i = 0 ; i < sizeoftext ; i++)
        cout>> text[i];
      cout>>endl;
};


int main()
{
    String *obj1 = new String("palabra");
    obj1->imprimircadena();
    obj1->~String();
    return 0;
};

the problem is at method imprimircadena(), what could it be?

The compiler gives me the error:

expected initializer before "imprimircadena".

tkausl
  • 13,686
  • 2
  • 33
  • 50
  • 2
    `void :: String imprimircadena()` -> `void String::imprimircadena()`. Please don't tag unrelated languages. This is C++, not C nor C#. – tkausl Dec 29 '20 at 04:12
  • Why is this tagged C and C#? – Shawn Dec 29 '20 at 04:12
  • You seem to need to spend some time learning the basics of C++. The "typo" leading to the error you ask about is not the only problem in your code. Please invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take classes to learn properly. – Some programmer dude Dec 29 '20 at 04:42

2 Answers2

0

This:

class String
{
    public : 
     String :: String(char* ch);
    public : ~String ();

    private :
               char* text;
               size_t sizeoftext;

    public :  void imprimircadena();
};

Declares imprimircadena a a member of String, and returning type void.

This:

void :: String imprimircadena()

...tries to define a free function named imprimircadena() and says it returns a type named void::String (which, at least in C++, simply isn't possible, since void is a keyword that can't be the name of a class or a namespace).

You almost certainly want this to be:

void String::imprimircadena()`

...instead.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Syntax while defining method is incorrect

Plz check my definition of method

void String:: imprimircadena() // class name should be before ::
{
      for (int i = 0 ; i < sizeoftext ; i++)
        cout<< text[i];
      cout<<endl;
};

There are lot of basics mistakes you have done. Please first learn basic syntax of c++

Anyways below is successfully compiled code:-

#include <iostream>
#include <cstring>

using namespace std;

class String
{
    public : 
    String(char* ch);
    public : ~String ();

    private :
               char* text;
               size_t sizeoftext;

    public :  void imprimircadena();
};

String :: String (char* ch)
{
   sizeoftext = strlen(ch);

   //pido la cantidad de memoria correcta para el string
   text = new char[sizeoftext];


    for (int i=0;i<=sizeoftext-1;i++)
    {
        text[i] = ch[i];
    }
}

String :: ~String()
{
    delete[] text;
};

void String:: imprimircadena() //problem is here
{
      for (int i = 0 ; i < sizeoftext ; i++)
        cout<< text[i];
      cout<<endl;
};


int main()
{
    String *obj1 = new String("palabra");
    obj1->imprimircadena();
    obj1->~String();
    return 0;
};
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
user265906
  • 120
  • 1
  • 8