-1

Hoping that someone could provide some insight into the below 'error' that VS code is flagging in my C++ code.

I recently got a new laptop (Macbook Pro M1 Pro chip) so I have set up my environment now. Everything looks good, however I seem to be getting reference initialisation and expected expression errors in a for loop that I wasnt previously getting - My other computer (Mac Mini M1 chip) does not have this issue and doesnt flag any errors.

To note: The full code compiles fine, so there shouldn't be any issue with it, but interested to hear peoples thoughts and potentially a way to get rid of the error appearing?

Example code block (I have a few for loops that are similar but serve different purposes, they are all showing the same error):

Wallet &Blockchain::GetChainWallet(string WalletAddress)
{
    for (Wallet &ChainWallet : _vWallets)
    {
        if (WalletAddress == ChainWallet.GetWalletID())
        {
            return ChainWallet;
            break;
        }
    }
}

The errors I'm receiving are:

reference variable "ChainWallet" requires an initializer

expected an expression

Full:

[{
    "resource": "/Users/carlbrand/Documents/GitHub/BRIK/src/cpp/Blockchain/Blockchain.cpp",
    "owner": "C/C++",
    "code": "252",
    "severity": 8,
    "message": "reference variable \"ChainWallet\" requires an initializer",
    "source": "C/C++",
    "startLineNumber": 70,
    "startColumn": 30,
    "endLineNumber": 70,
    "endColumn": 31
}]

[{
    "resource": "/Users/carlbrand/Documents/GitHub/BRIK/src/cpp/Blockchain/Blockchain.cpp",
    "owner": "C/C++",
    "code": "29",
    "severity": 8,
    "message": "expected an expression",
    "source": "C/C++",
    "startLineNumber": 70,
    "startColumn": 41,
    "endLineNumber": 70,
    "endColumn": 42
}]

Is there anything wrong with what I'm doing? I just want to get rid of the errors :)

Thanks in advance.

rioV8
  • 24,506
  • 3
  • 32
  • 49
Calleb213
  • 151
  • 1
  • 9
  • hmm... fantastic to be downvoted without a comment to say why... really helps me to learn and progress. That is after all why I posted this question... – Calleb213 Dec 08 '21 at 11:09
  • 1
    It looks like you're compiling for too old a standard. Are you sure that you're compiling in the same way with the same toolchain on both machines? (Also, what does the function return if it can't find a match?) – molbdnilo Dec 08 '21 at 11:10
  • Thanks for insight @molbdnilo - so I dont use VS code to compile, I compile outside in terminal - I compile this code with std=c++11 in terminal - so is VS code using an older linter then? I will have a look at the config in vs. – Calleb213 Dec 08 '21 at 11:15
  • @molbdnilo - So my compiler is giving me a warning about this, and I've been wondering how I can do it as a separate issue. I am new to C++ but have had a lot of exposure to Python/JS so i'm struggling with a few fundamentals of the lang - if it doesnt find a match i want to effectively do the equivalent of return None in python but i've not worked out how to do it in C++... And it will ultimately throw an exception if there is no match. – Calleb213 Dec 08 '21 at 11:19
  • 1
    what is the purpose of the `break;` – rioV8 Dec 08 '21 at 11:46
  • I would recommend that you get [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start from the beignning. C++ is fundamentally *very* different from both Python and JavaScript. – molbdnilo Dec 08 '21 at 11:48
  • tag with the compiler used, VSC is not the one that generates the error – rioV8 Dec 08 '21 at 11:49
  • @rioV8 thanks - realise thats not necessary as return statement will break out of the loop in itself - removed now. – Calleb213 Dec 08 '21 at 12:01
  • @molbdnilo - oh yea, fully aware it is very different fundamentally. I am reading several resources, coding for me is purely educational, I just find it highly entertaining and a fantastic learning opportunity. I learn by doing, practicing writing different code. This whole project is a learning project so i'm working out as I go, as this is the way I learn best. Thanks for the book list! That will be very helpful :) – Calleb213 Dec 08 '21 at 12:01
  • If you really want to learn, you should try a different paradigm, such as functional or logic programming. – molbdnilo Dec 08 '21 at 12:15
  • thanks @molbdnilo - what language would you suggest? – Calleb213 Dec 08 '21 at 12:47

1 Answers1

0

Apologies for the stupid question - as @molbdnilo advised in the comments, my configuration in vs code was set to too low of a standard - updated to c++11 and all looks good.

Thanks I'll try to refrain from stupid questions in future :)

Calleb213
  • 151
  • 1
  • 9