expected member name or ';' after declaration specifiers
vscode is giving me a few weird errors in the vec3.h file:
expected` '(' gcc[13,25]
expected ';' after expression gcc[13,25]
expected member name or ';' after declaration specifiers gcc [13,27]
That is referring to the vec3 constructor
I've tried copy & pasting the code directly and it throws the same problem. I'm new to c++ (I want to learn a bit) so not completely sure about the syntax. I am wondering if this is to do with the c++ task.json or launch.json files? Should I avoid using vscode for c++ in general?
A bit of a turn off to c++ as a newbie! Gimme my c# back ha
#ifndef VEC3_H
#define VEC3_H
#include <cmath>
#include <iostream>
using std::sqrt;
class vec3
{
public:
vec3() : e{0,0,0} {}
vec3(double e0, double e1, double e2) : e{e0, e1, e2} {}
double x() const { return e[0]; }
double y() const { return e[1]; }
double z() const { return e[2]; }
vec3 operator-() const { return vec3(-e[0], -e[1], -e[2]); }
double operator[](int i) const { return e[i]; }
double& operator[](int i) { return e[i]; }
vec3& operator+=(const vec3 &v) {
e[0] += v.e[0];
e[1] += v.e[1];
e[2] += v.e[2];
return *this;
}
vec3& operator*=(const double t) {
e[0] *= t;
e[1] *= t;
e[2] *= t;
return *this;
}
vec3& operator/=(const double t) {
return *this *= 1/t;
}
double length() const {
return sqrt(length_squared());
}
double length_squared() const {
return e[0]*e[0] + e[1]*e[1] + e[2]*e[2];
}
public:
double e[3];
};
vscode is meant more as a smart text editor than as a compiler. Visual Studio is a smart text editor and compiler. As vscode got more popular, people started integrating compiler features into it. As your error all have the gcc tag in them, I assume that you have a gcc plugin. Looking over the code, and as you said that you copy/pasted it, I don't see any problems with your code.
I'm afraid there isn't much that I'm able to help with. Maybe someone else can jump in. I assume that your problem is vscode related. If you have access to Visual Studio, you may want to compile your program there. As you are a C# developer, I assume that you're probably a windows developer, if so, the options for compiling c++ get pretty rough. I'm personally not a fan of the win command line. But you can try to access your gcc through command prompt. I can help with any of these options.
The best I can tell you is that you should probably avoid vscode for compilation. You are the first time I've personally run into anyone compiling their code in vscode, and it's failing. sooooo.....
Thanks for your reply. That's a bit surprising to hear that vscode isn't very popular with this. I'm actually on macOS - I had followed this tut for setting up https://code.visualstudio.com/docs/cpp/config-clang-mac
The main thing that the tutorial seems to do is add a tasks.json and launch.json to the project
I had actually started Ray tracing in one weekend in vscode before I had added those files, and I had no problems building and progressing with the stages up until the vec3.h stage.. where i think i arbitrarily decided to follow the official vscode tutorial
In case it's helpful to see what the tasks and launch files contained:
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/clang++"
}
]
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ build active file"
}
]
}
I'm afraid I'm out of my depth here. You could try compiling your program from the command line with clang
Can you try replacing
vec3() : e{0,0,0} {}
vec3(double e0, double e1, double e2) : e{e0, e1, e2} {}
with
vec3() { e[0] = 0; e[1] = 0; e[2] = 0; }
vec3(double e0, double e1, double e2) {
e[0] = e0;
e[1] = e1;
e[2] = e2;
}
and see if that works? I think those are the lines the compiler is complaining about. If not, then I'd immediately fall back on commenting out code and simplifiying until I knew exactly where the problem is.
That worked! I wasn't quite brave enough to to change the syntax, so thanks for doing that.
vscode is now warning 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
Also warning about use of alias
I have c++17 so not sure what that's about. Nevermind.
Thanks!
That's a very strange warning. It really looks like you have C++17 enabled for the shell, but not for the two builds that follow. This seems like some kind of configuration issue.
Same problem on MacOS, and solved with adding "set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20")" to CMakeLists.txt.
Refer to: https://stackoverflow.com/questions/72781742/class-constructor-is-not-assigning-values-correctly
@AlecHang — thank you for following up! I'm trying to figure out whether
set (CMAKE_CXX_STANDARD 20)
is sufficient, or whether I have to also use
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20")
It seems redundant to have to do the second line in addition to the first. I would hope that setting the C++ standard would include C++ compiler flags.
Also note that the current C++ standard is set at C++11, which I think is sufficient. I would love to have someone verify failure with the code presented in the book, and then report back if changing the CMAKE_CXX_STANDARD line from 11 to 14/17/20 fixes things.
@hollasch your #1390 commit is going backwards in time. Brace initialization has been valid since C++11 (for over 10 years).
Judging by the warning "'auto' type specifier is a C++11 extension" their compiler/project was running in C++03 mode.
Also note that the current C++ standard is set at C++11, which I think is sufficient.
You are correct. I can find relevant section from the standard, if you don't trust my word. 😃
And, since we are on this subject, there is actually no need to have constructors for simple classes such as vec3, ray and interval. You can just use brace initialization.
Yeah, that's what I thought as well. I've run into oddness before between Visual Studio, Clang, and GCC offering different levels of support, particularly with respect to the C++ Standard Library (which I don't think comes into play for this issue). It is a bit odd that only one reader has reported issues with this particular usage of brace initialization.
All that said, I kind of prefer this approach because it's easier for non-C++ readers to grok. Don't know — I'm kind of torn on this one. Let me think about it.
Since this should be ok with C++11, I'll close this for now, and we'll see if anybody else reports problems with their setup.