-1

Here is a working code

#include "quickfix/FixFields.h"
#include "quickfix/Values.h"

int main()
{
  FIX::BeginString beginString(FIX::BeginString_FIX42);
  return 0;
}

It compiles and if I print the value of beginString I get the expected result.

Now I want to implement a class with the same type

#include "quickfix/FixFields.h"
#include "quickfix/Values.h"

class A {
  FIX::BeginString beginString;
public:
  A()
  {
    beginString = FIX::BeginString_FIX42;
  }
};

int main()
{
  return 0;
}

This code does not compile, the compilation error is

test.cpp: In constructor ‘A::A()’:
test.cpp:9:17: error: no match for ‘operator=’ (operand types are ‘FIX::BeginString’ and ‘const char [8]’)
     beginString = FIX::BeginString_FIX42;
                 ^

Any idea what is wrong and how to fix it?

e271p314
  • 3,841
  • 7
  • 36
  • 61
  • The problem is that `FIX::BeginString_FIX42` is not a type. In the first code sample, you are instantiating something, in the class you are declaring a function. – juanchopanza Jul 29 '14 at 19:36
  • I don't know what you're trying to do, but I'm 99% sure you are doing it wrong. So what are you actually trying to do? – Grant Birchmeier Jul 29 '14 at 20:03
  • I want to define a class with a data member of type `FIX::BeginString`. I don't understand why the compiler is willing to assign the correct value to a variable in `main()` but won't compile the code that tries to do the same when the variable is a data member of a class. – e271p314 Jul 29 '14 at 20:11
  • 1
    To initialise the member, why not use the constructor's initialiser list? That works if (as here) there's a conversion constuctor but no assignment operator. – Mike Seymour Jul 29 '14 at 23:28
  • Not only your comment is useful, it is in fact the answer to my problem. I updated my answer and give you all the credit. – e271p314 Jul 30 '14 at 09:52
  • Did you look at the sample code provided with the `QuickFix` library ? – DumbCoder Jul 30 '14 at 10:44

2 Answers2

0

FIX::BeginString_FIX42 is a const i'm guessing. When you define a function which again I am guessing you want to do you need to specify a type of a value not a value. BeginString is a type BeginString_FIX42 is a constant that tells begin string which version of the begin to return.

rerun
  • 25,014
  • 6
  • 48
  • 78
  • I don't want to define a function, I want to define a member. What I don't understand is why it was so easy when I was not using `class A` but when I put it inside a class I can't compile this code and everything I tried doesn't seem to work – e271p314 Jul 29 '14 at 19:53
0

As suggested by Mike Seymour using the initializer list of the constructor can be used when conversion operator exists but assignment doesn't.

#include "quickfix/FixFields.h"
#include "quickfix/Values.h"

class A {
  FIX::BeginString beginString{FIX::BeginString_FIX42};
};

int main()
{
  return 0;
}
e271p314
  • 3,841
  • 7
  • 36
  • 61