-5

I'm working on a project in Visual Studio that includes a "portable" menu GUI for anyone who wants it in their own work. Since I keep getting the C2797 compiler error, I can't use structs at all since nothing has helped and I use classes a lot.


Basically, I can't use structs, even though it's the most important part of my project.

Menu.h:

struct menuItem
{
    const char *name;
    bool value;
};

Test.h:

#include "Menu.h"

class Test
{
public:
    menuItem dummy = { "Useless option", false }; // <--This is where I get C2797
};

Again, this won't compile, but if I use this exact code in a simple C++ console app, it works flawlessly on all compiler versions.


If you need more details, when I use structs outside of classes, I get C4430 intertwined with C2440 and C2065. (And adding cstdint doesn't help that) If I try using the exact same struct in the closest situation possible in a different project, it compiles with all compiler versions just fine.

For the people who might think that it's because of the VS 2013 compiler (Which I'm using), I've already switched between newer and older versions between all of my projects and it made no difference.

For the other people who didn't see my details in the comments: Pastebin of errors in the console

  • 5
    You see error messages. We cannot see them. Why? – IInspectable Dec 20 '16 at 18:56
  • That's why I'm completely confused. I'm building my project off of a base, but that shouldn't be able to affect how structs work. – Cade Martinez Dec 20 '16 at 18:57
  • 1
    I am sorry, but I didn't reach the level of knowing what the error is about, just by looking at the error code. Can you provide us, with error messages, as well? – Algirdas Preidžius Dec 20 '16 at 18:57
  • 2
    Copy the text of the error message from the Output tab. – drescherjm Dec 20 '16 at 18:59
  • 1
    [This may help](https://msdn.microsoft.com/en-us/library/dn793970.aspx) – Borgleader Dec 20 '16 at 19:01
  • Here's a picture of my code and the console: [Imgur link](http://i.imgur.com/7tkMifW.png) – Cade Martinez Dec 20 '16 at 19:07
  • And here's the entire console output: [Pastebin link](http://pastebin.com/KzgBsj5i) – Cade Martinez Dec 20 '16 at 19:09
  • Possible duplicate of [RDTSC on VisualStudio 2010 Express - C++ does not support default-int](http://stackoverflow.com/questions/18417942/rdtsc-on-visualstudio-2010-express-c-does-not-support-default-int) – Nikola Lozanovski Dec 20 '16 at 19:11
  • @Borgleader That's exactly what I was linked to when I first asked the question elsewhere. I'm not using any arrays in my structure. The compiler is fine the the structure, but not when when I use it. – Cade Martinez Dec 20 '16 at 19:11
  • @CadeMartinez: The error message never even talks about arrays. It talks about non-static data member initializers, for which list initialization isn't supported (which you are using). Changing that to `menuItem dummy = menuItem{ "Useless option", false };` might fix it. – IInspectable Dec 20 '16 at 19:16
  • @NikolaLozanovski I only get those errors when I use structs outside a class, and include csdint didn't help. I still get just as many of the same errors. – Cade Martinez Dec 20 '16 at 19:17
  • @IInspectable I also tried that before I asked my question. I tried a lot of things that hasn't helped so far. – Cade Martinez Dec 20 '16 at 19:20
  • You still missed the point I was trying to make in my first comment: Please provide the full, unabridged error message in your question. Questions without a clear problem statement are off-topic. – IInspectable Dec 20 '16 at 19:24
  • The error messages need to go [here](http://stackoverflow.com/posts/41249330/edit). Please take the [tour] and visit the [help]. – IInspectable Dec 20 '16 at 19:31
  • @IInspectable They are there. At the bottom in large letters – Cade Martinez Dec 20 '16 at 19:34
  • ***Error messages***, not just error codes. – IInspectable Dec 20 '16 at 19:35
  • @IInspectable Are the descriptions of the codes not next to them? That's all I have. The console is the most information of the errors I have. – Cade Martinez Dec 20 '16 at 19:40
  • When I said *"need to go [here](http://stackoverflow.com/posts/41249330/edit)"* I really meant it. Questions (and answers) on Stack Overflow are supposed to be self-contained. No links to off-site resources should contain vital information. – IInspectable Dec 20 '16 at 19:44

2 Answers2

0

The fix, as the documentation for the error states, is to explicitly construct the struct on the RHS of the assignment rather than trying to get the compiler to deduce the type of the brace-initializer (which it is unable to do correctly). Like this:

class Test
{
public:
    menuItem dummy = menuItem{ "Useless option", false }; // Note the extra menuItem
};
zdan
  • 28,667
  • 7
  • 60
  • 71
  • Using that doesn't help, as stated in the comments of my main post. When I use that I don't get my main error, but I get the same results as using menuItem outside classes (Which results in more errors, but not C2797). – Cade Martinez Dec 20 '16 at 19:25
  • @CadeMartinez: So it **does** address this specific error, but your code has another error, that only shows up after fixing this one. You **really** need to add the full details to your question (and take the [tour], probably). – IInspectable Dec 20 '16 at 19:27
  • Well then you should post code that shows those other errors. But from those error codes, it sounds like the compiler is not seeing the declaration of the type you are using. – zdan Dec 20 '16 at 19:30
-2

I believe it's because you're not allowed to initialize variables inside the public: unless they are static. Maybe if you try

public:
    static const menuItem={"Useless option", false };   // might work
  • Doing this automatically gives me "A member of type "const menuItem" cannot have an in-class initializer" along with the errors I get from using menuItem outside of classes – Cade Martinez Dec 20 '16 at 19:27
  • This is trying to solve the problem by changing the problem, rather than an attempt to solve it. – IInspectable Dec 21 '16 at 13:14