-3

To start, I'm very new to c++ and Linux, so if you could keep that in mind when you respond that would be great :)

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

Can anyone explain to me whats going on?

cadaniluk
  • 14,769
  • 2
  • 38
  • 64
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
  • 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 appearing in a separate header file.


I'd suppose what yo want is

#include <iostream>

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

int main(){

}
πάντα ῥεῖ
  • 85,314
  • 13
  • 111
  • 183