Solved

Problem passing a vector class pointer as reference

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 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

13 Experts available now in Live!

Get 1:1 Help Now