Solved

Problem passing a vector class pointer as reference

Posted on 2008-06-12
5
1,314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

707 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