Solved

Problem passing a vector class pointer as reference

Posted on 2008-06-12
5
1,310 Views
Last Modified: 2013-12-14
Using an example of code below; if I run the "main.h" and "main.cpp" file by themselves all works fine. Unfortunately I need to view the vector's object from another file. I can only guess that I need to pass the vector by reference. After searching and trying many many things I'm still unsure of "the call", "the phototype" and "fuction definition" to make this work. Below are four files of the example. Any suggestions?

//main.h
#ifndef MAIN_H
#define MAIN_H

#include <iostream>
#include <iomanip>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using namespace std;

class Shape {
public:
      void setCenterX(int x);
      int getCenterX() { return x; }

      void setCenterY(int y);
      int getCenterY() { return y; }
      
      virtual void printShapeName() const = 0;
      virtual void printCenter() const;
      virtual void print() const = 0;
      virtual void printArea() const = 0;

private:
      int x;
      int y;
};

void Shape::setCenterX(int cX) { x = cX; }

void Shape::setCenterY(int cY) { y = cY; }

void Shape::printCenter() const { cout << "Location=" << '[' << x << "," << y << ']'; }

class Circle : public Shape {
public:
      Circle( int x = 0, int y = 0, double r = 0.0 );
      double getRadius() const { return radius; }
      virtual void printShapeName() const { cout << "Circle:"; }
      virtual void print() const;
      virtual void printArea() const;
            
private:
      double radius;
};
#endif

//main.cpp
#include "main.h"
//#include "view.cpp"
#include <iostream>
#include <iomanip>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using namespace std;
using std::ios;
using std::setiosflags;
using std::setprecision;

Circle::Circle( int cX, int cY, double cR )
{
      setCenterX(cX);
      setCenterY(cY);
      radius = cR;
}

void Circle::print() const { cout << ", radius=" << radius; }

void Circle::printArea() const { cout << ", Area=" << 3.14159 * radius * radius << endl; }

int main()
{
      cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
      vector<Shape *> sh;      
      sh.push_back(new Circle(1, 1, 2.0));
      
      //this prints fine
      int i = 0;
      sh[i]->printShapeName();
      sh[i]->printCenter();
      sh[i]->print();
      sh[i]->printArea();
      cout << endl << endl;

      return 0;
}

//view.h
#ifndef VIEW_H
#define VIEW_H

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

class viewShape {
public:
      void viewTheShape(vector<Shape *> &);

private:      
};
#endif

//view.cpp
#include "main.h"
#include "view.h"
#include <iostream>
#include <iomanip>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using namespace std;
using std::ios;
using std::setiosflags;
using std::setprecision;

void viewTheShape(vector<Shape *> &sha)
{
      cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
      int i = 0;
      sha[i]->printShapeName();
      sha[i]->printCenter();
      sha[i]->print();
      sha[i]->printArea();
      cout << endl << endl;
}
0
Comment
Question by:Enview
  • 3
  • 2
5 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 21776115
at view.cpp, this line:
void viewTheShape(vector<Shape *> &sha)

should be:
void viewShape::viewTheShape(vector<Shape *> &sha)
0
 

Author Comment

by:Enview
ID: 21776150
jaime olivares,
I made the change, now receiveing:
error LNK2005: "public__void thiscall Shape::setCenterX(int)" (?setCenterX@Shape@@QAEXH@Z already defined in main.obj.     Another same error for setCenterY and PrintCenter

I'm really not sure what to think?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21776211
have you removed the comment slashes before #include "view.cpp" ?
0
 

Author Comment

by:Enview
ID: 21776272
Yes, and changed it to view.h    ...... also remove/changed the three lines in main.h
void Shape::setCenterX(int cX) { x = cX; }   ** moved the last part up: void setCenterX(int cX) { x = cX; }
void Shape::setCenterY(int cY) { y = cY; }  **moved the last part up: void setCenterY(int cY) { y = cY; }
void Shape::printCenter() const { cout << "Location=" << '[' << x << "," << y << ']'; }  **moved the last part up: virtual void printCenter() const { cout << "Location=" << '[' << x << "," << y << ']'; }

all complies fine with no LNK errors.
Also, I added these two lines to the "main.cpp" file
      viewShape V;
      V.viewTheShape(sh);
Everything works great! Thanks for stepping me through the problem!
0
 

Author Closing Comment

by:Enview
ID: 31466826
Thank you for stepping me through this problem.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticallā€¦
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called eeā€¦
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question