• پایان فعالیت بخشهای انجمن: امکان ایجاد موضوع یا نوشته جدید برای عموم کاربران غیرفعال شده است

مشکل با برنامه های چند فایلی

parvaneh_s

کاربر تازه وارد
تاریخ عضویت
29 جولای 2006
نوشته‌ها
18
لایک‌ها
0
سلام
ممنون بابت اطلاعاتی که میدید. من یه مشکل با لینک کردن برنامه های چند تا فایلی دارم.
برنامه اصلی بدون خطا هست. ولی وقتی build رو میزنم خطا داره:
ain.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Cyliner::print(void)const " (?print@Cyliner@@UBEXXZ)

توی همه برنامه های چند فایلی که مینویسم مشابه همین خطا رو داره . اشکال میتونه از کجا باشه؟ ممنون اگه منو از این سر درگمی دربیارن!:)
 

parvaneh_s

کاربر تازه وارد
تاریخ عضویت
29 جولای 2006
نوشته‌ها
18
لایک‌ها
0
مشکل فقط مربوط به برنامه هایی که خودم مینویسم نیست. مثالهای کتاب دایتل رو از روی سی دی خود کتاب، کپی کردم باز هم توی build برنامه خطا داره. مربوط به تنظیمات ویژوال نیست؟
 

apache**

کاربر تازه وارد
تاریخ عضویت
15 جولای 2006
نوشته‌ها
26
لایک‌ها
0
محل سکونت
Tehran
سلا م
برنامه های ماژولار رو شما باید یه جوری به هم وصل کنید
من با ++C برنامه ماژولار ننوشتم اما با C این کارو کردم
من دو اصل رو رعایت می کنم:
1- ایجاد یک فایل header که حاوی prototype همه توابع و دستور include همه کتابخانه های مورد استفاده
در برنامه و احتمالاً structure و در ++C حتماً class های تعریف شده!
وسپس در همه thread های برنامه فقط فایلی که خودم ساختم include میکنم!

2- کامپایل هم زمان همه فایلها بجز فایل header این امکان در محیط های ویندوزی مثلvisual studio
با ایجاد پروژه و اضافه کردن فایل فراهم میشه !

اگر این دو اصل رعایت بشه دیگه تداخل و دوگانگی ایجاد نمیشه!

یا حق
 

parvaneh_s

کاربر تازه وارد
تاریخ عضویت
29 جولای 2006
نوشته‌ها
18
لایک‌ها
0
ممنون از راهنماییهای همه. ولی من باز هم نتونستم مشکل رو رفع کنم. منظورتون از اینکه همشو با هم کامپایل کنم اینه که همه فایلهای .cpp رو با هم باز کنم و کامپایل کنم؟ اگه منظور اینه ، اینکارو کردم ولی بازم توی build خطا داره.:blink:
از نظر درست بودن برنامه ها مطمئن هستم. چون با برنامه های دایتل هم امتحان کردم. توی build اونا هم همین خطا رو داره.:blink:
 

parvaneh_s

کاربر تازه وارد
تاریخ عضویت
29 جولای 2006
نوشته‌ها
18
لایک‌ها
0
ممنون از لینکی که داده بودید. اون توی توربو c بود. توی vc++ هم گفته بود پروژه MFC رو ایجاد کرد.چه طوری میشه اینکارو کرد؟
این یکی از فایلهای هدر برنامه هست. که بقیه هدرها از اون به ارث میبرند. البته این هدر به اسم shape.h ذخیره شده. توی فایلshap.cpp این رو includeکردم.




ifndef SAHPE_H#
define SHAPE_H#


;using std::string

class Shape
}

: pubic
;virtual string getName() const =0
; ()virtual double getVolume const
; virtual void print() const =0
;virtual double getArea() cons
; virtual void print() const ;{

[LEFT/]

[INDENT/]

توی فایل اصلیم هم تمامی هدر فایلها رو include میکنم.
نحوه کامپایل کردنم هم اینجوری هست که هر دفعه که یک فایل .cpp مینویسم اونرو کامپایل میکنم. vc++ رو میبندم و فایلهای بعدیم رو به همین نحو مینویسم. فایل اصلی رو هم به همین شیوه کامپایل و بیلد میکنم.​
 

mohammad_110

کاربر فعال برنامه نویسی
کاربر فعال
تاریخ عضویت
22 ژانویه 2006
نوشته‌ها
60
لایک‌ها
0
اگه کل برنامه رو اتچ کنید ببینیم چیه بهتره .
ولی توی این کلاس یک تابع دو بار با یک اسم تعریف شده بود که یکیش تابع تجریدی بود یکی هم معمولی و هم نام بودند متغییرای دریافتی هم که نداشتند. ممکن مشکل از اون باشه.
 

parvaneh_s

کاربر تازه وارد
تاریخ عضویت
29 جولای 2006
نوشته‌ها
18
لایک‌ها
0
ببخشید توی کد بالا اشتباه از من بود که دو بار اون قسمت رو کپی کردم!
کد یکی از برنامه های دایتل رو میذارم:
این کلاسی هست که بقیه کلاسها ازش به ارث میبرند:
کد:
#ifndef SHAPE_H
#define SHAPE_H

#include <string>  // C++ standard string class

using std::string;

class Shape {

public:
   
   // virtual function that returns shape area
   virtual double getArea() const;

   // virtual function that returns shape volume
   virtual double getVolume() const;

   // pure virtual functions; overridden in derived classes
   virtual string getName() const = 0; // return shape name
   virtual void print() const = 0;     // output shape

}; // end class Shape

#endif
[code/]
 

parvaneh_s

کاربر تازه وارد
تاریخ عضویت
29 جولای 2006
نوشته‌ها
18
لایک‌ها
0
اینم پیاده سازی کلاس قبلی:

کد:
// Fig. 10.13: shape.cpp
// Shape class member-function definitions.
#include <iostream>  

using std::cout;

#include "shape.h"  // Shape class definition

// return area of shape; 0.0 by default
double Shape::getArea() const
{
   return 0.0;

}  // end function getArea

// return volume of shape; 0.0 by default
double Shape::getVolume() const
{
   return 0.0;

}

[code/]
------------------------------------------------------------------
اینم یکی از کلاسهایی که ازش به ارث میبره:

[code]

#ifndef POINT_H
#define POINT_H

#include "shape.h"  // Shape class definition

class Point : public Shape {

public:
   Point( int = 0, int = 0 ); // default constructor

   void setX( int );  // set x in coordinate pair
   int getX() const;  // return x from coordinate pair
   
   void setY( int );  // set y in coordinate pair
   int getY() const;  // return y from coordinate pair
   
   // return name of shape (i.e., "Point" )
   virtual string getName() const;

   virtual void print() const;  // output Point object

private: 
   int x;  // x part of coordinate pair
   int y;  // y part of coordinate pair

}; // end class Point

#endif

[code/]
---------------------------------------------------------
پیاده سازی :

[code]

#include <iostream>

using std::cout;

#include "point.h"   // Point class definition

// default constructor
Point::Point( int xValue, int yValue )
   : x( xValue ), y( yValue )
{
   // empty body 

} // end Point constructor

// set x in coordinate pair
void Point::setX( int xValue )
{
   x = xValue; // no need for validation

} // end function setX

// return x from coordinate pair
int Point::getX() const
{
   return x;

} // end function getX

// set y in coordinate pair
void Point::setY( int yValue )
{
   y = yValue; // no need for validation

} // end function setY

// return y from coordinate pair
int Point::getY() const
{
   return y;

} // end function getY

// override pure virtual function getName: return name of Point
string Point::getName() const
{
   return "Point";

}  // end function getName

// override pure virtual function print: output Point object
void Point::print() const
{
   cout << '[' << getX() << ", " << getY() << ']';

} // end function print

[code/]
---------------------------------------------------------
2 تا کلاس دیگه هم ازش به ارث میبرن:
[code]

#ifndef CIRCLE_H
#define CIRCLE_H

#include "point.h"  // Point class definition

class Circle : public Point {

public:

   // default constructor
   Circle( int = 0, int = 0, double = 0.0 );  

   void setRadius( double );   // set radius
   double getRadius() const;   // return radius

   double getDiameter() const;       // return diameter
   double getCircumference() const;  // return circumference
   virtual double getArea() const;   // return area

   // return name of shape (i.e., "Circle")
   virtual string getName() const;

   virtual void print() const;  // output Circle object

private: 
   double radius;  // Circle's radius

}; // end class Circle

#endif 

[code/]
-----------------------------------------------------------
پیاده سازی:
[code]

#include <iostream>  

using std::cout;

#include "circle.h"   // Circle class definition

// default constructor
Circle::Circle( int xValue, int yValue, double radiusValue )
   : Point( xValue, yValue )  // call base-class constructor
{
   setRadius( radiusValue );

} // end Circle constructor

// set radius 
void Circle::setRadius( double radiusValue )
{
   radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );

} // end function setRadius

// return radius 
double Circle::getRadius() const
{
   return radius;

} // end function getRadius

// calculate and return diameter
double Circle::getDiameter() const
{
   return 2 * getRadius();

} // end function getDiameter

// calculate and return circumference
double Circle::getCircumference() const
{
   return 3.14159 * getDiameter();

} // end function getCircumference

// override virtual function getArea: return area of Circle
double Circle::getArea() const
{
   return 3.14159 * getRadius() * getRadius();

} // end function getArea

// override virutual function getName: return name of Circle
string Circle::getName() const
{
   return "Circle";

}  // end function getName

// override virtual function print: output Circle object
void Circle::print() const
{
   cout << "center is ";
   Point::print();  // invoke Point's print function
   cout << "; radius is " << getRadius();

} // end function print
[code/]
----------------------------------------------------------------
 اون یکی کلاس دیگه تقریبا مثل همینه.
این هم تابع اصلی:
[code]

#include <iostream>

using std::cout;
using std::endl;
using std::fixed;

#include <iomanip>

using std::setprecision;

#include <vector>

using std::vector;

#include "shape.h"     // Shape class definition
#include "point.h"     // Point class definition 
#include "circle.h"    // Circle class definition 
#include "cylinder.h"  // Cylinder class definition

void virtualViaPointer( const Shape * );  
void virtualViaReference( const Shape & );

int main()
{
   // set floating-point number format
   cout << fixed << setprecision( 2 );

   Point point( 7, 11 );                  // create a Point
   Circle circle( 22, 8, 3.5 );           // create a Circle
   Cylinder cylinder( 10, 10, 3.3, 10 );  // create a Cylinder

   cout << point.getName() << ": ";    // static binding
   point.print();                      // static binding
   cout << '\n';

   cout << circle.getName() << ": ";   // static binding
   circle.print();                     // static binding
   cout << '\n';

   cout << cylinder.getName() << ": "; // static binding
   cylinder.print();                   // static binding
   cout << "\n\n";

   // create vector of three base-class pointers
   vector< Shape * > shapeVector( 3 );  

   // aim shapeVector[0] at derived-class Point object
   shapeVector[ 0 ] = &point;

   // aim shapeVector[1] at derived-class Circle object
   shapeVector[ 1 ] = &circle;

   // aim shapeVector[2] at derived-class Cylinder object
   shapeVector[ 2 ] = &cylinder;

   // loop through shapeVector and call virtualViaPointer
   // to print the shape name, attributes, area and volume 
   // of each object using dynamic binding
   cout << "\nVirtual function calls made off "
        << "base-class pointers:\n\n";

   for ( int i = 0; i < shapeVector.size(); i++ ) 
      virtualViaPointer( shapeVector[ i ] );

   // loop through shapeVector and call virtualViaReference
   // to print the shape name, attributes, area and volume 
   // of each object using dynamic binding
   cout << "\nVirtual function calls made off "
        << "base-class references:\n\n";

   for ( int j = 0; j < shapeVector.size(); j++ ) 
      virtualViaReference( *shapeVector[ j ] );

   return 0;

} // end main

// make virtual function calls off a base-class pointer
// using dynamic binding
void virtualViaPointer( const Shape *baseClassPtr )
{
   cout << baseClassPtr->getName() << ": ";

   baseClassPtr->print();

   cout << "\narea is " << baseClassPtr->getArea()
        << "\nvolume is " << baseClassPtr->getVolume() 
        << "\n\n";

} // end function virtualViaPointer

// make virtual function calls off a base-class reference
// using dynamic binding
void virtualViaReference( const Shape &baseClassRef )
{
   cout << baseClassRef.getName() << ": ";

   baseClassRef.print();

   cout << "\narea is " << baseClassRef.getArea()
        << "\nvolume is " << baseClassRef.getVolume() << "\n\n";

} // end function virtualViaReference

[code/]
---------------------------------------------

البته کلاس cylinder رو ننوشتم چون مشابه circle هست فقط یه تابع اضافه برای محاسبه حجم داره.اگه نیازی بود بگید.ممنون از توجهتون
 
بالا