0

I am trying to print the first value from each vector shown below in the main function.

#include <iomanip>
#include <map>

using namespace std;

typedef unsigned int vect;

int main() {
    std::vector<vect> p;

    vector<vect> a = { 4,2,3,1 };
    vector<vect> b = { 4,2,3,1 };
    vector<vect> c = { 4,2,3,1 };
    vector<vect> d = { 4,2,3,1 };

    int i;
    for (i=0; i<a.size(); i++)
    cout << a[i];
}

Function first_preference() from my function.cpp shown below

#include "function.h"
#include <string>
#include <iostream>

using namespace std;

 person test::first_preference() const {

        const person& first = p.front();
        return first; //current first pref
    }

The function is declared in this header class

#ifndef FUNCTION_H
#define FUCNTION_H

#include <vector>

typedef unsigned int person;
typedef unsigned int vect;

std::vector<vect> p;


class test {

public:
    person first_preference() const;
};

#endif

I want the function first_preference() to be called from main() where the function should print the first value of each vector, how would I go about this?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
s_1921
  • 9
  • 3
  • 1
    Voting to close as a typo. `&vote::first_preference` is the address of the member function, not a call to it. – Ted Lyngmo Nov 20 '19 at 23:28
  • How are functions called? – s_1921 Nov 20 '19 at 23:34
  • `instance.member_function(arguments)` is one way. Make your code into a [mcve] and it'll be easier to help. – Ted Lyngmo Nov 20 '19 at 23:36
  • @TedLyngmo I've simplified the question hopefully it will be easier to help? I'm trying to call the function the way you advised but it wasn't working – s_1921 Nov 21 '19 at 00:06
  • 1
    You oversimplified. Your simplified code has no `test` object, so a non-static member of `test` cannot be called. – JaMiT Nov 21 '19 at 00:47

1 Answers1

2

I want the function first_preference() to be called from main() where the function should print the first value of each vector

Some issues:

  • You have a global std::vector<vect> p in your header file (which is not a good idea to begin with) which is shadowed by std::vector<vect> p in main. What you put in the p in main will not be accessible from instances of test. Those instances only knows about the global p.

  • You don't #include "function.h" in main.cpp so you can't create test objects in main.

  • If you #include "function.h" in main.cpp there's no need to typedef unsigned int vect; since you did that in function.h already. It's not an error, but confusing and unnecessary.

  • The vector<vect> instances a, b, c and d have no connection with test or any of the ps whatsoever so what you put in those vectors can't possibly be printed by instances of test unless you pass them on to test somehow.

  • You declare vectors of vect but first_preference() returns a person by value. vect and person happen to be aliases of the same fundamental type, but it seems like there is something wrong with this interface.

  • In main.cpp you don't instantiate a test, you iterate over a and first_preference() is never called so there's no hope for it to be used.

  • Why is “using namespace std;” considered bad practice?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108