Solved

Problem passing a vector class pointer as reference

Posted on 2008-06-12
5
1,301 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
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…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now