• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1364
  • Last Modified:

C++ In constructor no matching function for call to

Hi I have a problem when compiling my c++ program.  I just recently made some changes and apparently ran into something I don’t understand about c++.

FreeEMS_LoaderComms.cpp: In constructor ‘FreeEMS_LoaderComms::FreeEMS_LoaderComms()’:
FreeEMS_LoaderComms.cpp:21: error: no matching function for call to ‘TNX::QSerialPort::QSerialPort()’

They only thing I can guess is that by putting this line(TNX::QSerialPort serPort;) into the header is causing its constructor to be called when my constructor is called.  Any input would be appreciated.
thx in advance
.....
FreeEMS_LoaderComms::FreeEMS_LoaderComms() :
     verifyLastWrite(false), verifyACKs(false), flashTypeIndex(0), fDeviceIsSet(false), smIsReady(false)
{
  init();
  flushMode = false;
}

...

header file
....
 #include <QSerialPort>
 TNX::QSerialPort serPort;
......

Open in new window

0
sean-keys
Asked:
sean-keys
3 Solutions
 
käµfm³d 👽Commented:
With this line

TNX::QSerialPort serPort;

Open in new window


you are attempting to instantiate a new object; however, the error message is letting you know that there is no "default" (parameterless) constructor for the QSerialPort class. Check the definition of the class and see which constructor options you have available.
0
 
sean-keysAuthor Commented:
ah, let me see what one of its constructors requires.  I suppose i was misled by the fact that it flagged an error in another file.
0
 
trinitrotolueneCommented:
>>>"They only thing I can guess is that by putting this line(TNX::QSerialPort serPort;) into the header is causing its constructor to be called when my constructor is called"
Not really. I can't see your FreeEMS_LoaderComms::FreeEMS_LoaderComms() constructor calling this QSerialPort constructor.

Check whether you need to place "serPort" in your header file in the first place?  Is it a member variable of some other class?

Does it form part of some object you would be using?

The reason for the error ofcourse is as kaufmed has mentioned the lack of a default constructor.

Here's what you can do once you've pondered my points above

- use a constructor which is provided for QSerialPort when you define serPort.

so for eg: you might find a constructor which takes the port no. as constructor. Use this in your header file instead.

int aPortNum;
TNX::QSerialPort serPort(aPortNum);


But as I said before definitions should ideally not be done in a header file.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sarabandeCommented:
you could make the TNX::QSerialPort serPort a member of your class FreeEMS_LoaderComms. then if the QSerialPort takes a port number as assumed by trinitrotoluene your constructor would change like

FreeEMS_LoaderComms::FreeEMS_LoaderComms() :
     verifyLastWrite(false), verifyACKs(false), flashTypeIndex(0), fDeviceIsSet(false), smIsReady(false), serPort(12345)
{
  init();
  flushMode = false;
}

Open in new window


Sara
0
 
sean-keysAuthor Commented:
Check whether you need to place "serPort" in your header file in the first place?  Is it a member variable of some other class? no

Does it form part of some object you would be using? yes

I tried the code below but it gives me the following errors.

FreeEMS_LoaderComms.h: In constructor ‘FreeEMS_LoaderComms::FreeEMS_LoaderComms()’:
FreeEMS_LoaderComms.h:348: error: ‘FreeEMS_LoaderComms::smIsReady’ will be initialized after
FreeEMS_LoaderComms.h:347: error:   ‘bool FreeEMS_LoaderComms::fDeviceIsSet’
FreeEMS_LoaderComms.cpp:19: error:   when initialized here
FreeEMS_LoaderComms.h:347: error: ‘FreeEMS_LoaderComms::fDeviceIsSet’ will be initialized after
FreeEMS_LoaderComms.h:345: error:   ‘int FreeEMS_LoaderComms::flashTypeIndex’
FreeEMS_LoaderComms.cpp:19: error:   when initialized here



Then I tried just changing my header file to what a valid constructor expects
 // TNX STUFF
  void open(QString serPortName);
  TNX::QSerialPort serPort("115200,8,n,1");
but get  

FreeEMS_LoaderComms.h:255: error: expected identifier before string constant
FreeEMS_LoaderComms.h:255: error: expected ‘,’ or ‘...’ before string constant
make[1]: *** [debug/FreeEMS_LoaderThreads.o] Error 1
make[1]: Leaving directory `/home/seank/work/workspaceCDT/freeems-loader/src'

Sorry guys :(  
thx!
using namespace std;

FreeEMS_LoaderComms::FreeEMS_LoaderComms() :
     verifyLastWrite(false), verifyACKs(false),   serPort("115200,8,n,1"),smIsReady(false),
     fDeviceIsSet(false), flashTypeIndex(0)
{
  init();
  flushMode = false;
}

Open in new window

0
 
sarabandeCommented:
the 'serPort' must be made member of class FreeEMS_LoaderComms or it cannot be used in the initialiser list of the constructor. you also need to include the <QSerialPort> header in your class header.


can you post your class header and the class definition of TNX::QSerialPort?

Sara
0
 
sean-keysAuthor Commented:
"the 'serPort' must be made member of class FreeEMS_LoaderComms or it cannot be used in the initialiser list of the constructor" -  by this do you mean it needs to be inherited ?

"you also need to include the <QSerialPort> header in your class header" -it already is.

I the past I use to create a pointer like this in my header
TNX::QSerialPort *serPort;
and in the constructor function for the class I wanted to use it in I would do a "new", but I'm not sure that's the correctly way of doing it.  I read you want to avoid using "new" as much as possible.

header file attached below, sorry it's messy I haven’t used c++ in 15 years.

Thanks!!!
#ifndef FREEEMS_LOADERCOMMS_H_
#define FREEEMS_LOADERCOMMS_H_

#include <stdexcept>
#include <string>
#include <sstream>
#include <iostream>
#include <QSerialPort>
#include <QString>
#include "FreeEMS_LoaderSREC.h"

#include <QObject>

#define SM_READY_CHAR_SIZE      0x03
#define ONE_TWENTY_EIGHT_K_RECORDS     128000  //enough records to hold 4MB at 16bytes each
using namespace std;

/**
 * Thrown if timeout occurs
 */
class timeout_exception : public std::runtime_error

{
public:
  timeout_exception(const std::string& arg) :
    runtime_error(arg)
  {
  }
  // timeout_exception(const std::string& arg)  std::cout<<arg {}
};

/**
 * Serial port class, with timeout on read operations.
 */
class FreeEMS_LoaderComms : public QObject
{
Q_OBJECT
public:
  FreeEMS_LoaderComms();
  /**
   * Opens a serial device. By default timeout is disabled.
   * serial device
   */

  /*
   *  B7/DC/IDID — Returns the constant $DC (Device C=12) and the 2-byte
   *  HCS12 device ID register. Please refer to selected device guides for device ID
   *  register contents.
   */
  void
  ripDevice();

  void
  loadDevice();

  void
  init();

  void
  clearSets();

  void
  generateRecords(vector<string> lineArray);

  bool
  lineIsLoadable(string* line);

  void
  setThreadAction(int action);

  void
  setRipFilename(QString name);

  void
  setLoadFilename(QString name);

  void
  resetSM();

  int
  getDeviceByteCount();

  void
  loadRecordSet();

  void
  SMWriteByteBlock(unsigned int address, char* bytes, int numBytes);

  //void SMSetLoadAddress(unsigned int address, unsigned int typeID, int numBytes);

  /*
   *  B7/DC/IDID — Returns the constant $DC (Device C=12) and the 2-byte
   *  HCS12 device ID register. Please refer to selected device guides for device ID
   *  register contents.
   */
  void
  returnFlashType(char *responce);

  void
  setFlashType(const char *commonName);

  void
  SMSetPPage(char PPage);

  void
  SMReadByteBlock(unsigned int address, char plusBytes, std::vector<char> &vec);

  void
  SMReadChars(const char *data, size_t size);

  void
  flushRXStream();

  /*
   * Read a block of memory starting at address specified.
   * Block is read twice to check integrity.
   * 256 bytes max
   */

  int
  verifyReturn();

  //bool
  //verifyReturn(std::vector<char> &vec);

  /**
   * Opens a serial device.
   */
  void open(QString serPortName, unsigned int baud_rate);

  /**
   * \return true if serial device is open
   */
  bool
  isReady() const;
  /**
   * \return true if serial device is open
   */
  bool
  isOpen() const;

  /**
   * Close the serial device
   * \throws boost::system::system_error if any error
   */
  void
  close();

  /**
   * Set the timeout on read/write operations.
   * To disable the timeout, call setTimeout(boost::posix_time::seconds(0));
   */
//  void
//  setTimeout(const boost::posix_time::time_duration& t); //TODO reimpliment

  /**
   * Write data
   * \param data array of char to be sent through the serial device
   * \param size array size
   * \throws boost::system::system_error if any error
   */
  void
  write(const char *data, size_t size);

  /**
   * Write data
   * \param data to be sent through the serial device
   * \throws boost::system::system_error if any error
   */
  void
  write(const std::vector<char>& data);

  void
  write(const char *data);

  /**
   * Write a string. Can be used to send ASCII data to the serial device.
   * To send binary data, use write()
   * \param s string to send
   * \throws boost::system::system_error if any error
   */
  void
  writeString(const std::string& s);

  /**
   * Read some data, blocking
   * \param data array of char to be read through the serial device
   * \param size array size
   * \return numbr of character actually read 0<=return<=size
   * \throws boost::system::system_error if any error
   * \throws timeout_exception in case of timeout
   */
  void
  read(char *data, size_t size);

  /**
   * Read some data, blocking
   * \param size how much data to read
   * \return the receive buffer. It iempty if no data is available
   * \throws boost::system::system_error if any error
   * \throws timeout_exception in case of timeout
   */
  std::vector<char>
  read(size_t size);

  /**
   * Read a string, blocking
   * Can only be used if the user is sure that the serial device will not
   * send binary data. For binary data read, use read()
   * The returned string is empty if no data has arrived
   * \param size hw much data to read
   * \return a string with the received data.
   * \throws boost::system::system_error if any error
   * \throws timeout_exception in case of timeout
   */
  std::string
  readString(size_t size);

  /**
   * Read a line, blocking
   * Can only be used if the user is sure that the serial device will not
   * send binary data. For binary data read, use read()
   * The returned string is empty if the line delimiter has not yet arrived.
   * \param delimiter line delimiter, default="\n"
   * \return a string with the received data. The delimiter is removed from
   * the string.
   * \throws boost::system::system_error if any error
   * \throws timeout_exception in case of timeout
   */
  std::string
  readStringUntil(const std::string& delim = "\n");

  void
  erasePage(char PPage);

  void
  eraseDevice();

  void
  setSM();

  bool verifyLastWrite;
  bool verifyACKs;

  // TNX STUFF
  void open(QString serPortName);
  TNX::QSerialPort serPort("115200,8,n,1");
  TNX::QPortSettings serPortSettings;

  ~FreeEMS_LoaderComms();

signals:
  void
  WOInfo(string text);
  void
  udProgress(int percent);
  void
  configureProgress(int min, int max);

private:

  /**
   * Parameters of performReadSetup.
   * Just wrapper class, no encapsulation provided
   */
  class ReadSetupParameters
  {
  public:
    ReadSetupParameters() :
      fixedSize(false), delim(""), data(0), size(0)
    {
    }

    explicit
    ReadSetupParameters(const std::string& delim) :
      fixedSize(false), delim(delim), data(0), size(0)
    {
    }

    ReadSetupParameters(char *data, size_t size) :
      fixedSize(true), delim(""), data(data), size(size)
    {
    }

    //Using default copy constructor, operator=

    bool fixedSize; ///< True if need to read a fixed number of parameters
    std::string delim; ///< String end delimiter (valid if fixedSize=false)
    char *data; ///< Pointer to data array (valid if fixedSize=true)
    size_t size; ///< Array size (valid if fixedSize=true)
  };

  /**
   * This member function sets up a read operation, both reading a specified
   * number of characters and reading until a delimiter string.
   */
  void
  performReadSetup(const ReadSetupParameters& param);

  /**
   * Callack called either when the read timeout is expired or canceled.
   * If called because timeout expired, sets result to resultTimeoutExpired
   */
//  void
//  timeoutExpired(const boost::system::error_code& error); //TODO reimpliment

  /**
   * Callback called either if a read complete or read error occurs
   * If called because of read complete, sets result to resultSuccess
   * If called because read error, sets result to resultError
   */
//  void
//  readCompleted(const boost::system::error_code& error,
//      const size_t bytesTransferred);

  /**
   * Possible outcome of a read. Set by callbacks, read from main code
   */
  enum ReadResult
  {
    resultInProgress, resultSuccess, resultError, resultTimeoutExpired
  };
  enum ReadResult result; ///< Used by read with timeout
  size_t bytesTransferred; ///< Used by async read callback
//  ReadSetupParameters setupParameters; ///< Global because used in the OSX fix

  FreeEMS_LoaderSREC *s19SetOne;
  FreeEMS_LoaderSREC *s19SetTwo;

  int s19SetOneCount;
  int s19SetTwoCount;
  unsigned int lastLoadAddress;

  QString ripFilename;
  QString loadFilename;

  int flashTypeIndex;
  int threadAction;
  bool fDeviceIsSet;
  bool smIsReady;
  bool flushMode;
};

#endif /* FREEEMS_LOADERCOMMS_H_ */


Serial port header


#ifndef TNX_QSERIALPORT_H__
#define TNX_QSERIALPORT_H__

#include "qserialportnative.h"
#include "qserialport_export.h"

namespace TNX {

/**
 * Represents a serial port resource.
 */
class TONIX_EXPORT QSerialPort : public QSerialPortNative
{
Q_OBJECT

private:
  enum {
    kReadNotifyThresholdLimit = 1024,
  };

public:
  /**

Open in new window

0
 
sarabandeCommented:
no, a member of a class means a data member (attribute, property) like in

class FreeEMS_LoaderComms : public QObject
{
     ...
private:
     QSerialPort serPort;   // the class has 'serPort' as member

Open in new window


the above is a private member what is normal if the member was accessed only by member functions of the class. if FreeEMS_LoaderComms is a baseclass, you can make it protected by adding

protected:

above the member declaration (don't forget to reset to private: or public: after).

Sara
0
 
sarabandeCommented:
if you are not so familiar with C and C++ i would strongly recommend to not use pointers.

in C++ it is often unnecessary. The serPort member can be initialized in the initializer list of the constructor of FreeEMS_LoaderComms as i showed above and you shouldn't use a pointer for the only reason that it can be initialized a little bit easier.

unfortunately you posted not the full header of qserialport.h. so i couldn't verify what arguments are needed for the constructor.

Sara
0
 
sean-keysAuthor Commented:
I'm decent with C.  And have written a couple thousand lines of c++, but I cant help but think I'm missing some important features.

"C++ it is often unnecessary" exactly that was a big motive for me to open this question in the first place.

I'll see what i can do with your member example.
thx!


0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now