Visual Studio editor thinks that my declared vector is actually a declared function

Nov 2, 2018 at 12:41am
I am trying to create a class that, when instantiated in to an object, will place a number of randomly generated elements in to a vector. Right off the bat, the Visual Studio editor thinks that my declared vector variable is actually a function. When I hover over "listOfNums", I get this error:

"std::vector<int> SortedArray::listOfNums(size_t numOfElements)
Function definition for 'listOfNums' not found."

Shouldn't the C++ compiler recognize that "listOfNums" is using a constructor to take size "numOfElements"?

Here is my code:

1
2
3
4
class SortedArray{
   private:
		std::vector<int> listOfNums(size_t numOfElements);
}


Last edited on Nov 2, 2018 at 12:42am
Nov 2, 2018 at 12:56am
Don't just "hover". Compile it and post all of the error output (including any that scrolled off the top). The error presumably begins somewhere else.
Nov 2, 2018 at 1:14am
closed account (1vRz3TCk)
If you want do define a function that takes a size_t and returns a vector of int, how would you write it?
Nov 2, 2018 at 1:25am
Okay will do. Will let you all know what the error is.
Nov 2, 2018 at 1:45am
closed account (1vRz3TCk)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include <vector>

class SortedArray
{
public:
	SortedArray(size_t numberOfElements = 5);

	std::vector<int> listOfNumbers;
};

SortedArray::SortedArray(size_t numberOfElements)
	: listOfNumbers(numberOfElements)
{

}

int main()
{
	SortedArray sa1;
	SortedArray sa2(19);

	std::cout << "sa1 capacity: " << sa1.listOfNumbers.capacity() << "\n";
	std::cout << "sa2 capacity: " << sa2.listOfNumbers.capacity() << "\n";

	return 0;
}
sa1 capacity: 5
sa2 capacity: 19
Nov 2, 2018 at 2:07am
Yeah, I wasn't even thinking, man. That's a function declaration. You need to do it like CodeMonkey shows.
Nov 2, 2018 at 4:57am
Took your advice, CodeMonkey. I looked up member initialization in my textbook and created the constructor you recommended.

Topic archived. No new replies allowed.