Am I Declaring Functions Correctly?

  • Thread starter Lancelot59
  • Start date
  • Tags
    Vectors
In summary, your #include lines are wrong, and you're trying to define a function with the wrong name. The problem stems from the fact that you can declare (but not define) one function inside of another.
  • #1
Lancelot59
646
1
I'm currently learning C++, and I'm trying to use vectors and some simple loops to find the largest/smallest values out of 10 inputted values:

Code:
#include iostream
#include string

using namespace std;

int main()
{

	vector<double> Values();
	double Largest, Smallest, TempVal;
	int i;
	
	//Getting the input
	while(data.size() < 11)
	{
		cout << "Input a number and press the enter key: ";
		cin >> TempVal;
		Values.push_back(TempVal);
	};
	
	//Finding The Largest Value
	for(i = 0, Largest = Values[0]; i < 10; i++)
	{
		if(Values[i] > Largest)
		{
			Largest = Values[i]
		};
	};
	
	//Finding The Smallest Value
	for(i = 0, Smallest = Values[0]; i < 10; i++)
	{
		if(Values[i] < Smallest)
		{
			Smallest = Values[i]
		};
	};
	
	cout << "The largest value is: " << Largest << endl;
	cout << "The smallest value is: " << Smallest << endl;

	return(0);
};

However I keep getting some random compile errors related to the vector. What am I doing wrong here?
 
Technology news on Phys.org
  • #2
It would help if you posted the error -- both to save us time, and so we can help teach you to read them.


But anyways, the first thing I notice is that your #include lines are wrong.

Another thing I notice is that I think the declaration
vector<double> Values();
is bad. You shouldn't have the parentheses there. I believe that makes the compiler think you are trying to prototype a function.
 
  • #3
Possibly that, but I didn't realize vectors had their own library. :p

Thanks, everything works now. I'll be sure to remove the parentheses, and be on the safe side.

Although when you define the vector wouldn't any compiler be looking for the name immediately after?
 
  • #4
Lancelot59 said:
Possibly that, but I didn't realize vectors had their own library. :p
Oh, I missed the fact you didn't #include <vector>!

Although when you define the vector wouldn't any compiler be looking for the name immediately after?
There is an ambiguity1 in C++ syntax. How would you declare a function whose name is return type is vector<int> and takes zero arguments? It would look like
vector<int> myfunc();
Note that this is exactly the same form as the line you wrote trying to define a variable of type vector<int> to be constructed with the default constructor, as you did with Values.


1: I lied -- the ambiguity is only in the intuitive sense. I'm pretty sure the C++ standard directs the compiler to interpret it as declaring a function.
 
  • #5
P.S. shouldn't your for loops start at 1 instead of 0?
 
  • #6
Although when you define the vector wouldn't any compiler be looking for the name immediately after?

The problem stems from the fact that you can declare (but not define) one function inside of another.

We're generally used to seeing this:

Code:
void foo();

int main()
{
  foo();
  return 0;
}

void foo() {}

But this is also just as valid:

Code:
int main()
{
  void foo();
  foo();
  return 0;
}

void foo() {}
 

Related to Am I Declaring Functions Correctly?

1. What are vectors and how are they used in science?

Vectors are mathematical quantities that have both magnitude (size) and direction. They are commonly used in science to represent physical quantities such as velocity, force, and displacement. Vectors are often used in calculations and equations to describe the motion and interactions of objects.

2. How do I know if I am using vectors correctly?

To ensure that you are using vectors correctly, make sure that you are representing both magnitude and direction accurately. Vectors should also follow the laws of vector addition and subtraction, and the direction should be specified relative to a reference point.

3. What are some common mistakes when working with vectors?

Some common mistakes when working with vectors include forgetting to specify direction, using the wrong reference point, and failing to use the correct units for magnitude. It is also important to be careful with vector addition and subtraction, as they are not commutative operations.

4. Can vectors be used in any scientific field?

Yes, vectors are used in various scientific fields such as physics, engineering, and computer science. They are also used in other disciplines such as economics and biology to represent quantities and relationships between variables.

5. How can I improve my understanding and use of vectors?

To improve your understanding and use of vectors, it is important to practice solving problems involving vectors and familiarize yourself with the different types of vector operations. You can also seek guidance from textbooks, online resources, and peers who have a strong understanding of vectors.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
9K
  • Programming and Computer Science
Replies
4
Views
930
  • Programming and Computer Science
Replies
3
Views
858
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
Back
Top