vendredi 30 août 2019

Why unknown type name 'Complejo'?

This is a simple example.

EXAMPLE 1 If for example I declare this code (with class Complejo in same file):

#include <iostream>
#include <string>

using namespace std;

int main()
{
  Complejo com(1,1);

  cout << com << endl;

}


class Complejo
{
private:
  double real;
  double imaginario;
public:
  //Complejo();
  Complejo(double real, double imaginario)
  {
    real = 1;
    imaginario = 2;
  }
  double getReal() { return real; }
  double getImaginario() { return imaginario; }
};

ostream &operator<<(ostream &stream, Complejo& comp)
{
  stream << comp.getReal() << " + " << comp.getReal()<< endl;
  return stream;
}

My compiler says me:

sobrecarga_ostream.cpp:15:3: error: unknown type name 'Complejo'

EXAMPLE 2 But If I create sobrecarga_ostream.cpp:

#include <iostream>
#include <string>
#include "Complejo.h"
using namespace std;

int main()
{    
  Complejo com(1,1);


  cout << com << endl;

}

and Complejo.h:

#include <iostream>

using namespace std;

class Complejo
{
private:
  double real;
  double imaginario;
public:
  //Complejo();
  Complejo(double real, double imaginario)
  {
    real = 1;
    imaginario = 2;
  }
  double getReal() { return real; }
  double getImaginario() { return imaginario; }
};

ostream &operator<<(ostream &stream, Complejo& comp)
{
  stream << comp.getReal() << " + " << comp.getReal()<< endl;
  return stream;
}

Then, it works well.

Why does not "main + class" work in the same file and if I separate files then works?

Thanks!

Aucun commentaire:

Enregistrer un commentaire