1

I'm new in C++, and have been given a (relatively) complex piece of code.

I want to call a function transmit() from a .h file in another .c file. The transmit() is in the file serviceUart.hpp which content looks like this:

serviceUart.hpp

class ServiceUart
{
public:
    ServiceUart();
  void ioConfig(); // sets io HW ports and pins. Only needed at first boot
    void ioInit(); //
    bool readTrigger();
    bool detectConnection() {return (m_rxPin.get()|| m_enabled);}
    bool startup();
    bool transmit(const char* s, uint16_t length, bool wait = false);

The file drvr.cpp is where I try calling the function. A snippet of what I think is relevant from that file looks like this:

drvr.cpp

#include "EHS5_drv.hpp"

char debug[] = "I got to here!";
transmit(debug,true);

I tried serviceUart.transmit and serviceUart::transmit, but no matter what I try, I get the error code `#20 identifier "transmit" is undefined". I guess I'm misunderstanding the syntax?

12
  • 2
    You need an object to call transmit() Commented Feb 28, 2019 at 13:13
  • 3
    To call a method you need an object of the class ServiceUart, I would suggest a good C++ book if this is a new concept to you. Commented Feb 28, 2019 at 13:13
  • 3
    Do you have a ServiceUart object instantiated? And note that you pass true for the length argument (and since you don't have such an overload, this is my guess as to the problem). Please edit your question to include the full and complete build output, copy-pasted as text. Commented Feb 28, 2019 at 13:14
  • 1
    I recognize by these comments, that I need to study more before posting. I will take @drescherjm 's proposal to heart. Im also new on SO. Should I delete the question, or can someone "shut it down" for me? Thanks for the comments. Commented Feb 28, 2019 at 13:21
  • 1
    @MartinRasmussen No need to do anything - you asked a question, you got your answer. This is how it's supposed to work. You might want to accept the answer which you like the most in order to mark your question answered. Commented Feb 28, 2019 at 16:31

2 Answers 2

2

Method bool transmit(const char* s, uint16_t length, bool wait = false); is defined in ServiceUart. You have to create an object of ServiceUart class and then call method transmit()

char debug[] = "I got to here!";

ServiceUart obj;
obj.transmit(debug,true);

Or with new

ServiceUart* obj = new ServiceUart();
obj->transmit(debug,true);
delete obj;

Don't forget to delete obj.

Sign up to request clarification or add additional context in comments.

1 Comment

You might want to put the normal object first and then the new example. Because the person is new, and God forbid if he comes from Java, Java and C++ behave are different in a hell lot of ways
2

You probably want:

char[] debug = "I got here";

ServiceUart serviceObg;
obj.transmit(debug, true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.