user2752471
user2752471

Reputation: 454

error: using-declaration for non-member at class scope using std::cout

I downloaded a c++ project and was able to compile it using a makefile generated by cmake.

However when I try to add my own series of .h files in one of the .hh files of the project I start to get a million of errors, one of them being:

error: using-declaration for non-member at class scope using std::cout;

When the .h file that contains using std::cout is used elsewhere it works, but when added to this project it gives this error.

What can be the problem?

using std::cout;
using std::endl;
class TextManager : public FileManager {
    public:
        TextManager (const char * filename);
        void scanFile (Image &image, Scene &scene);
        void scanObjectModel (Image &image, Scene &scene);
        void getImageData (Image &image);
        void getMaterialData (Scene &scene);
        void getLightData (Scene &scene);
        void getSphereData (Scene &scene);
        void getPlaneData (Scene &scene);
        void getTriangleData (Scene &scene);
        int getLineValue (int size);
        void getLineValue2 (float (&lineNumbers) [10], Scene &scene, int &lineNumbersIndex);
        void getVerticesValues (int initPos, Scene &scene);  
        private:
   std::string line;
   float fractionaryTenPowers [6];
};

Problem solved. Was the lack of a bracket to close the declaration of one of the classes that was causing it.

Upvotes: 10

Views: 15528

Answers (3)

Tapendra BC
Tapendra BC

Reputation: 1

Indeed, check if you have one open bracket in one of the member functions in the class declaration.

I did this in .h file;

class foo{
    void cat();
    void bar{
    void dog();
}

in .cc file I defined the member functions
void foo::cat(){
    std::cout<<"This is cat"<<std::endl;
}

void foo::bar(){
    std::cout<<"hello"<<std::endl;
}

void foo::dog(){
    std::cout<<"meow"<<std::endl;
}

But notice I used { instead of; for member function bar in .h file. That's causing the error. (At least for me).

Upvotes: -3

juicedatom
juicedatom

Reputation: 219

You can put it in the class as long as you put it under the public or private sections.

#include <iostream>

namespace CoolNamespace
{
  struct AnotherReallyLongClassName
  {
    int a = 75;
  };

  struct SomeReallyLongClassName
  {
    int a = 42;
  };
} // namespace CoolNamespace

class Widget
{

  // You can't do this though!
  // using ShorterName = CoolNamespace::SomeReallyLongClassName;

  public:

    // You can use a using statement inside of a class!
    using ShorterName = CoolNamespace::SomeReallyLongClassName;
    ShorterName foo;

    int get_another_name()
    {
      return bar.a;
    }

  private:
    // You can do it here also!
    using AnotherName = CoolNamespace::AnotherReallyLongClassName;
    AnotherName bar;
};

int main()
{
  Widget widget;
  std::cout << widget.foo.a << std::endl;

  // Also, if you can reference public using statements from the class definition.
  Widget::ShorterName thing;
  std::cout << thing.a << std::endl;

  // But you can't do this because it's private.
  // Widget::AnotherName name;

  return 0;
}

Upvotes: -1

Jonathan Wakely
Jonathan Wakely

Reputation: 171433

The error means you've done this:

struct Foo {
  using std::cout;
  ...
};

That's not valid C++, in a class body you can only add a using-declaration for members of base classes, not arbitrary names.

You can only add using std::cout at namespace scope or inside a function body.

Upvotes: 17

Related Questions