necessary help

help me why the following code give me error



t=-10:0.1:10;
T0=1;
V=1;
x=V*square(t);
w0=2*pi/T0;
index=0;
for k=1:2:5

index=index+4*V/(k*pi)*cos(k*w0*t/2/pi-pi/2);

end
plot(t,x,t,index,'linewidth',3)
legend('original signal','approximation')
axis([-4 4 -3 3])
er=abs(x-index);
figure
plot(t,er,'linewidth',3)
legend('error')
axis([-4 4 -3 3])
Last edited on
Please do not double post:
http://www.cplusplus.com/forum/general/85125/
The order of your classes is wrong; you now define Town before House.
Last edited on
so give me the order
You must either declare or define a class before you use it:
http://www.cplusplus.com/articles/Gw6AC542/
but they are declared all
how to fix in C++ program

Error 1 error LNK2001: unresolved external symbol _mainCRTStartup C:\Users\MICROSOFT\Documents\Visual Studio 2010\Projects\New folder\new\new\LINK
Well the first compiler error is an indication that you are using the symbol House without first declaring it. Nowhere above that line is there anything which tells the compiler what "House" means - you leave that until later in the file. Therefore, the compiler does not know it's a type, and tries to interpret it as an object name.

Typically, what you do when writing C++ code is to put your class definition into a header file, and then include that header file at the top of every source file that uses it. So, in this case, you might have:

House.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<string>

using namespace std;  // Not a good idea to do this in a header

class House{
private:
    string *nameowner;
    int *numroom;
        int *Rentingfee;
        Room *room;
        friend class Town;
public:
House(){
        setnameowner();
                setnumroom();
                Rentingfee=new int(0);}
                
        
        ~House(){
                cout<<"bye House"<<endl;
                delete []room;
        }
 
 
        void setnameowner(){
           string a;
           cout<<"enter the owner name of this house:";
           cin>>a;
           nameowner=new string(a);}
 
         const string *getnameowner() const{
                 return nameowner;}
 
        void setnumroom(){
                int b;
                cout<<"Enter the rooms number of this house:";
                cin>>b;
                numroom=new int(b);
                setRoom(b);
        }
 
        const int *getnumroom()const{
                return numroom;}
 
 
        void setRoom(int n){
                room=new Room[n];}
 
        void setRentingFee(int index){
                int m;
                cout<<"Enter the monthly Renting fee of this house:";
                cin>>m;
                Rentingfee=new int(m);
        }
 
        const int *getRentingFee()const{
                return Rentingfee;
        }
 
        void DisplayHouse(){
                cout<<"the house name is :"<<getnameowner()<<endl;
                cout<<"the Renting fee is : "<<getRentingFee()<<endl;
                cout<<"the number of rooms in this house is :"<<getnumroom()<<endl;
                cout<<"and the rooms details as follows:"<<endl;
                for(int i=0;i<*numroom;i++)
                        room[i].DisplayRoom();}
};      


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include<iostream>
#include<string>

#include "House.h"  // This takes all the code from House.h and includes it here

using namespace std;
 
class Town{
private:
    House *homes;
        bool *Rented;
        string *NameTown;
        static  int Num;
 
public:
        Town(string n ){
    setnametown(n);
    Rented= new bool [5];
        for ( int i=0;i<5;i++){
                Rented [i]=0;
                sethomes();
                Num++;} // end constructor
 
       ~Town();
        cout<<"bye Town"<<endl;
                delete []homes ;
                homes=0;} // end destructor
 
        void setnametown( string n ){
                cout<<"Enter the name of this Town ::";
                        cin>>n;
                        NameTown=new string(n);}
 
 
 
    const string *getNameTown() const{
                return NameTown;}
 
         int getNum(){
                 return Num;}
 
        void sethomes(){
                 homes=new House[5];}
 
 
        void Addhouse(House a ,int index){
                homes[index]=a;
setRented(index);
if( Rented[index]=1)
        homes[index]. setRentingFee(index);
else
        cout<<"No Rening fee"<<endl;}
 
 
        void setRented(int index){
                IsRented( index);}
      
        void IsRented(int index){
                bool a;
                cout<<"Is this house with index"<<index<<"rented?? "<<endl;
                        cout<<"if yes enter 1 else enter 0"<<endl;
       cin>>a;
           if(a){
                   Rented[index]=1;
                   cout<<"this house is rented"<<endl;}
           else
                   cout<<" this house is not rented !"<<endl;
        }
 
                
                
        void AvgRentFees(){
                int sum =0;
                for ( int i=0;i<5;i++)
                        sum+=*(homes[i].Rentingfee);
                cout<<"the Average Renting fee is "<<sum/5<<endl;}
 
 
 
        void Cheapest(){
                
                int mini=*(homes[0].Rentingfee);
                string name=*homes[0].nameowner;
                for( int i=0;i<5;i++)
                        if(mini>*(homes[i].Rentingfee)){
                                mini=*(homes[i].Rentingfee);
                                name=*(homes[i].nameowner);}
                        
                        cout<<"the least renting fee is "<<mini<<"and the name of its owner"<<name<<endl;}
 
 
void HomeArea(int index){
        int sum=0;
        int a=*( homes[index].numroom);
        for(int i=0;i<a;i++)
                sum+=homes[index].room[i].Area;
        cout<<"The Total Area of house index"<<index<<"IS;;"<<sum<<endl;}
 
 
void DisplayTowm(){
        cout<<"the name of this Town is "<<&getNameTown<<endl;
        cout<<"the number of objects in this Town is :"<<getNum()<<endl;
        cout<<"this Town includes 5 homes and their info as following :";
                for ( int i=0;i<5;i++){
                        cout<<"info of house "<<i<<" : :";
        Addhouse(homes[i],i);
        homes[i].DisplayHouse();}
};
 
class Room{
private:
    int Area;
        string roomname;
        friend class House;
        friend class Town;
public:
Room( int a, string n){
        setArea(a );
                setroomname( n);}
 
        ~Room(){
                        cout<<"bye room"<<endl;}
                        
        void setArea(int a){
                cout<<"Enter the area of this room ";
                
                cin>>a;
                Area=a;}
 
        int getArea(){
                return Area;}
 
        void setroomname(string n){
                cout<<"Enter the name of this room ";
                cin>>n;
                roomname=n;}
 
        string getnameroom(){
                return roomname;}
 
        void DisplayRoom(){
                cout<<"name of room :"<<&getnameroom<<endl;
                cout<<"room Area:"<<&getArea<<endl;
        }};
        
        void main()
{
    Town d("bayan");
}


That way, you've defined what "House" means before trying to use it.

Typically, you wouldn't keep the definitions of the House methods in the header file; you'd move them to a source file, so that the header would contain only method declarations:

House.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<string>

class House{
private:
    string *nameowner;
    int *numroom;
        int *Rentingfee;
        Room *room;
        friend class Town;
public:
        House();
        ~House();

        void setnameowner();
         const string *getnameowner() const;
 
        void setnumroom();
 
        const int *getnumroom()const;
 
        void setRoom(int n);
 
        void setRentingFee(int index);
 
        const int *getRentingFee()const
 
        void DisplayHouse()
};      


House.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<string>

using namespace std;

#include "House.h"

House::House(){
        setnameowner();
                setnumroom();
                Rentingfee=new int(0);
}             
        
House::~House(){
                cout<<"bye House"<<endl;
                delete []room;
}
 
void House::setnameowner(){
           string a;
           cout<<"enter the owner name of this house:";
           cin>>a;
           nameowner=new string(a);
}

// ... and the rest of the method definitions. 

That makes sure that only bits that other code NEEDS to know are in the header file.

You'd then need to do the same thing with Room, ensuring that Room is defined before it is used to declare an object.
Last edited on
so ok I did this now and I put 7 files
3 files .h
3 files .cpp
main.cpp

but it gives me::
Error 4 error LNK1120: 2 unresolved externals C:\Users\MICROSOFT\Documents\Visual Studio 2010\Projects\New folder\newclasses\Debug\newclasses.exe

Error 3 error LNK2001: unresolved external symbol "private: static int Town::Num" (?Num@Town@@0HA) C:\Users\MICROSOFT\Documents\Visual Studio 2010\Projects\New folder\newclasses\newclasses\Town.obj

Error 2 error LNK2001: unresolved external symbol "public: __thiscall Town::~Town(void)" (??1Town@@QAE@XZ) C:\Users\MICROSOFT\Documents\Visual Studio 2010\Projects\New folder\newclasses\newclasses\Town.obj

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Town::~Town(void)" (??1Town@@QAE@XZ) referenced in function _main C:\Users\MICROSOFT\Documents\Visual Studio 2010\Projects\New folder\newclasses\newclasses\main.obj



Error 3: You have a static variable that needs to be defined. In Town.cpp put
int Town::Num = 0;

Error 2&1: You forgot to define the destructor for Town in your Town.cpp file.
my code is true 100%
and I wrote it by the perfect way and my C++ program is perfect no prob in system or any thing but when debug it gives me error like this:

Error 1 error LNK1123: failure during conversion to COFF: file invalid or corrupt C:\Users\MICROSOFT\Documents\Visual Studio 2010\Projects\New folder\newclasses\newclasses\LINK


only this one error please tell me how to fix
use the Rebuild All option from the Build menu.
Topic archived. No new replies allowed.