0

first of all thank you to anyone that reads my question and special thanks to anyone that can offer advice.

I’m in the second week of CS 162 right now and our professor has just introduced us to classes. After following along to his code I have been completely unable to compile or use a class that I create.

In an attempt to understand my issue I have created three files: tomato.h, tomato_imp.cpp and tomato_driver.cpp.

as the name suggests tomato.h is the header file that defines a class “tomato”.

tomato_imp is the implementation file and tomato_driver tries to use the functions and definition of tomato to preform a simple operation.

tomato.h:


#ifndef TOMATO
#define TOMATO
 
class tomato
{
private:
    int tweight;
    
 
public:
    tomato(int weight=0);
 
    void setTomato(int weight);
 
    int getTomato() { return tweight}
    
};
 
#endif /*TOMATO*/

tomato_imp.cpp:

#pragma once;
#include "tomato.h"
 
//tomato constructor
tomato::tomato(int weight)
{
  setTomato(weight);
}
 
// tomato member function
void tomato::setTomato(int weight)
{
   tweight=weight;
}

tomato_driver.cpp:

#include <iostream>
#include "tomato.h"

int main(){

    tomato john;

    john(4);

    cout<<tomato::john.getTomato;
}

I am using a MacBook with OS X 10.15.5, I am using g++ to compile my files.

the header file compiles with the warning, clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated [-Wdeprecated].

When I try to compile the implementation file it gives me several errors:

  1. tomato_imp.cpp:5:9: error: redefinition of 'tomato' tomato::tomato(int weight) ^ ./tomato.h:11:2: note: previous definition is here tomato(int weight){}; ^
  2. tomato_imp.cpp:7:3: error: use of undeclared identifier 'setTomato'; did you mean 'tomato'? setTomato(weight); ^ ./tomato.h:5:7: note: 'tomato' declared here class tomato{ ^
  3. tomato_imp.cpp:11:14: error: out-of-line definition of 'setTomato' does not match any declaration in 'tomato' void tomato::setTomato(int weight) ^~~~~~~~~

I’m not sure what’s going on with these errors, all three files are saved in the same folder. I have commented out #pragma once and it still sends the exact same error messages.

This is slightly beyond the realm of Computer Science that I currently understand and any help would be greatly appreciated.

  • 2
    You lied about what's in your files, because the error says `tomato(int weight){};` but you said `tomato(int weight=0);` . Those are different! – user253751 Jun 30 '20 at 16:08
  • 1
    One issue is your constructor, it doesn't make sense to call `setTomato` from inside the objects constructor. Instead just use `tweight = weight`. – elliptic_hyperboloid Jun 30 '20 at 16:10
  • 1
    What are you expecting `john(4)` to do? As it is written it doesn't make any sense. – elliptic_hyperboloid Jun 30 '20 at 16:11
  • @ user253751 wow, thanks. I think you might have actually solved my problem. I had 2 copies of tomato.h one as a sublime text file and one as Xcode in a sub-directory i forgot about. After cleaning up my files I am still getting: clang: error: linker command failed with exit code 1 (use -v to see invocation) – sweet_lou_44 Jun 30 '20 at 16:18
  • @elliptic_hyperboloid Ok i changed the content of the tomato constructor to weight=weight. The idea behind john is that john is an instance of tomato and I am using the constructor on line 8 to set the weight of john to 8. Thanks for your help. – sweet_lou_44 Jun 30 '20 at 16:22
  • In that case you should be calling it as `john.setTomato(8);`. – elliptic_hyperboloid Jun 30 '20 at 16:26
  • This [example](https://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm) describes exactly what you are trying to do. – elliptic_hyperboloid Jun 30 '20 at 16:28
  • @elliptic_hyperboloid ok I will try following these instructions. Thank you for your help. – sweet_lou_44 Jun 30 '20 at 16:29
  • In your main function : std::cout< – John_Sharp1318 Jun 30 '20 at 16:34
  • writting : tomato::john.getTomato as is means that you want a pointer to the method "getTomato" of the attibute "john" of the class "tomato". – John_Sharp1318 Jun 30 '20 at 16:37
  • Couple of religious jokes I could make with John(4), but who comes to stack overflow for our witty senses of humour? – user4581301 Jun 30 '20 at 18:15
  • `#pragma once;` goes in the header. Not in the the CPP file. – Andy Jun 30 '20 at 18:38
  • There's a bit of disagreement of whether or not `#pragma once` [should be in a header either](https://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards). – user4581301 Jun 30 '20 at 19:00

1 Answers1

2

I think that with john(4) you meant to call the setter function; you can replace it with

john.setTomato(4);

Also, the way you use it later is incorrect, you can do

std::cout << john.getTomato(); // do not forget ()

Finally you should be able to compile your example with:

g++ -c tomato_imp.cpp -o tomato_imp.o
g++ tomato_imp.o tomato_driver.cpp -o tomato

And run it with

./tomato
NeverGoodEnough
  • 362
  • 1
  • 4
  • This fixed my code, when I run ./tomato my terminal returns 4. Thank you for taking the time to help a c++ beginner. – sweet_lou_44 Jun 30 '20 at 17:17
  • besides the obvious errors I think I was mainly struggling with the correct usage of g++ with options. I will read more about c++ classes as well as the g++ compiler. – sweet_lou_44 Jun 30 '20 at 17:21