-3

I am trying to create a class in one file that I will implement in the header of another file when I make an instance of that class.

My class file is

#include <iostream>

int main()
{

    class Box
    {
        public:

        int _width;
        int _length;
        int _height;

    };
}

I saved this as boxclass.h, but I did not compile it. I read somewhere that when I add this to my header file, I should just save it as a text file.

My other file, which I tried to include my box class in, is this:

#include <iostream>
#include "/home/cole/cpp/boxclass.h"

using namespace std;

int main()
{
    Box outer{3, 4, 5};
    Box inner{1, 2, 3};

    Box newB = outer-inner;

    cout << newB << endl;
}

When I try to compile this, I get these errors repeated many times with many different values:

/home/cole/cpp/Boxclass.h:442:864: warning: null character(s) ignored
/home/cole/cpp/Boxclass.h:442:1: error: stray ‘\1’ in program

What’s going on?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
drakzuli
  • 43
  • 6
  • 1
    Sounds like you need a good book. Look through the recommendations [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Greg Kikola Jan 29 '17 at 18:23
  • You should not have int main() in your header file. And what editor are you using to create these files? –  Jan 29 '17 at 18:23
  • 1
    According to the message, there's a null character in the *864th* column of line *442* in Boxclass.h. How exactly are you creating your files? – molbdnilo Jan 29 '17 at 18:43

1 Answers1

1

You have two definitions for the main() {} function. That's not compliant for any C++ compiled code.

Further, you have a local declaration of your class here:

int main(){

    class Box
    {
    public:

        int _width;
        int _length;
        int _height;

    };
}

You don't want this, but an out-of-scope declaration of class Box is appearing in a separate header file.


I'd suppose you want:

#include <iostream>

class Box {
public:
   int _width;
   int _length;
   int _height;
};

int main(){

}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190