VS Warning if Initialiser List Added

If I define the constructor without an initialiser list, there is no warning, but as soon as I add an initialiser list, I get the warning 'function definition not found'. When I run the code, it calls the constructor anyway, but the green squiggly line is annoying. What could be the cause of visual studio (2019 - 16.0.3 version) flagging this supposed warning?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Declaration in .h file:
struct ModelViewProjection
{
	float fov, aspect, near, far;
	glm::mat4 projection, view, model;

	ModelViewProjection(float field_of_view, float aspect_ratio, float zNear, float zFar);
	glm::mat4 GetMVP();
};

// Definition in .cpp file:
ModelViewProjection::ModelViewProjection(float field_of_view, float aspect_ratio, float zNear, float zFar)
	: fov(field_of_view), aspect(aspect_ratio), near(zNear), far(zFar),
	  projection(glm::perspective(glm::radians(fov), aspect, near, far)),
	  view(glm::translate(glm::mat4(1.0F), glm::vec3(0.0F, 0.0F, 0.0F))),
	  model(glm::translate(glm::mat4(1.0F), glm::vec3(0.0F, 0.0F, 0.0F))) {}
Post the code with the error, not the one without it.

Is the warning coming from the compiler or only from IntelliSense? IntelliSense doesn't implement C++'s parsing algorithm exactly, so can it sometimes make mistakes.
Post the code with the error, not the one without it.

That is the code that causes the warning.

Is the warning coming from the compiler or only from IntelliSense?

It's coming from IntelliSense, so maybe it can be attributed to the mistake you refer to.
Last edited on
Member initialization lists belong in the .hpp, not the .cpp.
The member initializer list is part of the definition of the constructor.

If the constructor is defined out of line in the implementation file,
that is where the member initializer list should be.
Duthomas said

Member initialization lists belong in the .hpp, not the .cpp.

I have separated the declaration and definition of constructors with initialiser lists before without any problems, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Declaration in .h file:
#pragma once

class Example
{
	int x, y, z;

	Example(int,int,int);
};

// Definition in .cpp file:
#include "Example.h"

Example::Example(int x, int y, int z)
	: x(x), y(y), z(z) {}

The above code works absolutely fine with no warnings, so I'm not sure that putting the initialisation list in the .h file is actually a necessity.

JLBorges said

If the constructor is defined out of line in the implementation file,
that is where the member initialiser list should be.

Okay so it must just be an error on IntelliSense's behalf. Thanks.


Heh, I was wrong. (Sorry.)

You can get the fancy quote header with the following syntax:

  [quote=Varrick]Zhu Li, do the thing![/quote]

Which produces:

Varrick wrote:
Zhu Li, do the thing!


:O)
No problem.

Varrick wrote:
You can get the fancy quote header with the following syntax:

There we go! Thanks lol.
Topic archived. No new replies allowed.