-1

I've declared a struct outside the main() function (with a few operator overloads for it).

I made a vector of the structs in the main() and then passed it by reference to another function.(node is the struct)

void openInsert(vector<node> &vec, node node)//insert nodes in least to greatest

I need to iterate over the the vector. I was trying to use a iterator but I'm getting an error when declaring it. (im using "using namespace std;")

vector<node>::iterator itr = vec.begin();

the error is :

 no suitable user-defined conversion from  
 "std::_Vector_iterator<std::_Vector_val<std::_Simple_types<node>>>" to 
 "std::_Vector_iterator<std::_Vector_val<std::_Simple_types<<error-type>>>>" exists 

also :

parameter "node" is not a type name 
  • 10
    Having both a type named `node`, and a function parameter named `node` (whose type is `node`), can only bring you a major world of hurt. – Sam Varshavchik Apr 03 '18 at 01:29
  • capitalize your type name, `struct Node` or `class Node` – Ryan Haining Apr 03 '18 at 01:37
  • this is embarrassing thank you for the correction changing the name worked, why should i capitalize? – Coleman Platt Apr 03 '18 at 01:44
  • @ColemanPlatt, several reasons. (1) You're Coleman Platt, not e e cummings (and even he occasionally used capitalization). (2) It's an extremely widely used convention. (3) It's a simple convention, that if followed, never results in a collision between a type name and a variable name, or between a type name and a function name. – David Hammen Apr 03 '18 at 02:18
  • 1
    @DavidHammen I agree it’s better to follow widely-used conventions like this one, but I disagree with point 1. The English convention there is to capitalize a person’s name, not the word “human”; it breaks the programming convention. – Daniel H Apr 03 '18 at 04:31
  • 2
    You really ought to avoid `using namespace std` - it is a bad habit to get into, and [can silently change the meaning of your program](/q/1452721) when you're not expecting it. Get used to using the namespace prefix (`std` is intentionally very short), or importing *just the names you need* into the *smallest reasonable scope*. – Toby Speight Apr 03 '18 at 12:26

2 Answers2

0

Your error stems from node node on the line

void openInsert(vector<node> &vec, node node)//insert nodes in least to greatest

In general it is not a good idea to have a type with the same name as a variable. It can lead to difficult to debug issues like this where the compiler is confused about whether you mean node the type or node the variable.

Common (and best) practice is to start your class and struct names with a capital letter. Changing the node type to Node will change the problem line to

void openInsert(vector<Node> &vec, Node node)//insert nodes in least to greatest

which should resolve your issue.

Caleth
  • 52,200
  • 2
  • 44
  • 75
0

If you are following a naming convention with small letters for class names, then name clashes can be avoided by using namespaces

#include <vector>

namespace mp{

class node{};

}

// when refering to the class, prefix with mp::
// when refering to a variable name, don't.
void foo(std::vector<mp::node>& vec, mp::node node){
    std::vector<mp::node>::iterator itr = vec.begin();
}

compilable example on ideone

default
  • 11,485
  • 9
  • 66
  • 102