-3

I would like to split my code and add one function. I wrote this code and now stuck with simple things _

After I put class in the header I got error in main that 'Point' - unknown type. Without header if I put all function in one file all working clear:

  • question how to split the code into header and main
  • why my function to add values doesn't work with a class

Thank you !

The clear code which need to change :

#include <iostream>
#include <cmath>
using namespace std;

class Point {
int x,y,z;
public:
       Point(int a, int b, int c)
{   x=a;
    y=b;
    z=c;
}
void print ()
{
    cout<<"The x value is : " << x << endl;
    cout<<"The y value is : " << y << endl;
    cout<<"The z value is : " << z << endl;
}

inline float norm ()
{
    float t = x*x + y*y + z*z;
    float s = sqrt(t);
    return s;
    
}
void negate ()
{
    x= x * -1; y= y * -1; z= z * -1;
}
};

int main ()
{
Point A1(1,2,3);
cout << "The function prints out: \n";
A1.print ();
cout<< "The result of norm is: " << A1.norm () << endl;
A1.negate ();
cout << "The result of negate: \n";
A1.print ();
return 0;
}
  • 1
    Looks like you're tripping over namespaces. You don't want to put `using namespace std` in a header ([to be honest you don't want to use that sucker at all](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)) because it forces a bad choice onto everybody, so either implement the functions in a second cpp file or add `std::` to the front of everything that needs it and add `#include `. – user4581301 Jun 29 '20 at 17:35
  • `set` could go either in the header file or in the main file. Where do you want it to go? – john Jun 29 '20 at 17:45
  • It's very hard to say why it didn't work when you put the code in the header file because you haven't posted that code in the header file. If you want help with code then you've got to post the actual code. The `set` code you did post has multiple errors that would be errors whether it was in a header file or not. – john Jun 29 '20 at 17:46
  • The main code is working fine - "using namespace std" - its choice for everyone - I not sure that changing of namespace into std:: change something - my problem in separating files and adding fucntion – David Carny Jun 29 '20 at 17:47
  • @john The last code for me in XCODE is working fine – David Carny Jun 29 '20 at 17:49
  • I think in header will be okay – David Carny Jun 29 '20 at 17:50
  • @DavidCarny Sorry but I refuse to believe that this code `cout<<"Please provide X,Y,Z values?: ">>cin>>x,y,z;` is working just fine, and that's only the first line. – john Jun 29 '20 at 17:50
  • @DavidCarny Well it goes into the header file in exactly the same way as the other functions you already successfully put into the header file. As I said it's impossible to diagnose errors in code I cannot see. – john Jun 29 '20 at 17:51
  • not Im talking about : – David Carny Jun 29 '20 at 17:52
  • Here's how the namespace gets you: `#include "header.h"` is before `using namespace std;`, so all of the code in header.h does not know that `cout` is an acceptable subsitute for `std::cout` – user4581301 Jun 29 '20 at 17:52
  • the last code - clear code – David Carny Jun 29 '20 at 17:52
  • @DavidCarny Sorry but you've lost me – john Jun 29 '20 at 17:52
  • So I think the point about `using namespace std;` is the real problem. – john Jun 29 '20 at 17:54
  • *error in main that 'Point' - unknown type* I cannot duplicate, but I do get the other mistakes resulting from namespaces..When I clean up the namespace issue by adding `std::`, the only remaining problems I see are warnings about `int`s being converted to `float`s, and that's nothing you should have to worry about at this stage of your education. – user4581301 Jun 29 '20 at 17:54
  • I eddited my post – David Carny Jun 29 '20 at 18:00
  • One more time - i have a code – David Carny Jun 29 '20 at 18:00
  • i need to split into to files header and main – David Carny Jun 29 '20 at 18:01
  • I dont know how to do that – David Carny Jun 29 '20 at 18:01
  • I don't understand why are you attacking me @John etc, I'm a newbie and I want to find an answer just write how you think it should be without notice I will find explain by myself and that's all. But no, you are starting writing to me that I have lost someone... – David Carny Jun 29 '20 at 18:04
  • for me its not difficult to change into std::cout<<"The z value is : " << z << std::endl; – David Carny Jun 29 '20 at 18:06
  • 1
    John's not attacking you. He's trying to get to the bottom of what your question is. No one seems to be able to reproduce your problem. Side note: Don't add more code until what you currently have works as expected. Adding more code when you already have bugs will only add to your your problems. – user4581301 Jun 29 '20 at 18:08

1 Answers1

2

Try implementing code like this (the conventional way):

main.cpp :

#include "point.hpp"
#include <iostream>

int main() {
    Point A1; // Point A1(1, 2, 3);
    std::cout << "The function prints out: \n";
    A1.print();
    std::cout << "The result of norm is: " << A1.norm() << std::endl;
    A1.negate();
    std::cout << "The result of negate: \n";
    A1.print();
    return 0;
}

point.hpp : (just has prototype declarations)

#ifndef POINT_HEADER_INCLUDE_GUARD
#define POINT_HEADER_INCLUDE_GUARD

#include <cmath>
#include <iostream>

class Point {
    int x, y, z;

public:
    Point();
    Point(int, int, int);
    void  print();
    float norm();
    void  negate();
    void  set();
};

#endif

point.cpp : (has definitions of the things declared in point.hpp)

#include "point.hpp"

Point::Point() { set(); }

Point::Point(int a, int b, int c) {
    x = a;
    y = b;
    z = c;
}

void Point::print() {
    std::cout << "The x value is : " << x << std::endl;
    std::cout << "The y value is : " << y << std::endl;
    std::cout << "The z value is : " << z << std::endl;
}

float Point::norm() {
    float t = x * x + y * y + z * z;
    float s = std::sqrt(t);
    return s;
}

void Point::negate() {
    x = x * -1;
    y = y * -1;
    z = z * -1;
}

void Point::set() {
    std::cout << "Please provide X,Y,Z values: ";
    std::cin >> x >> y >> z;
    std::cout << "The x value is : " << x << std::endl;
    std::cout << "The y value is : " << y << std::endl;
    std::cout << "The z value is : " << z << std::endl;
}

Compile using some command like g++ -o main main.cpp header.cpp -I ./. It may change depending on your compiler but in any case you need to compile and link both the files.

EDIT: For C++ files, prefer naming the file the same as the (main) class it contains. Also prefer keeping the file names as all-lowercase, even though class names contain capital letters. It is OK to use commonly known abbreviations, and/or omit the name of the containing directory if that would cause unnecessary repetition (e.g., as a common prefix to every file name in the directory) and the remaining part of the name is unique enough.

BTW the mess in your original question was not at all reproducible. We all were getting errors due to using namespace std;!

These threads are linked with this question:

  1. How to create my own header file in c++?
  2. What Should go in my Header File in C++?
brc-dd
  • 10,788
  • 3
  • 47
  • 67
  • 1
    The important take-away from the command line, regardless of what tools you use, is both cpp files need to be compiled and linked. – user4581301 Jun 29 '20 at 18:05