Makefile (using header files, main files, etc)

Hello all. So for our class we have just been doing codes within one script. Now we are using header files, along with two cpp files that we are supposed to use a Makefile with. I'm using visual studio 2010 express and am not sure how to go about doing this. Any help would be grateful as I have no idea how to check if it will properly compile. We had one lecture on this, and our professor seems to assume that we all have Linux or something (the schools computer OS) as she never really outlined how to do this in Windows.

Thanks for all the help

Cheers
Check out this thread.
http://www.cplusplus.com/forum/beginner/116779/

If it doesn't directly answer your questions, or doesn't address all aspects of your questions, feel free to ask.
Thank you so much, that thread is beyond helpful. Even more complete than the documentation page on classes, in my opinion
Ahh so for some reason I'm still not grasping this. Might be the 8 hours of physics I did today. Perhaps somebody could help guide me in the right direction, cause at this point even the instructions are reading like jargon to me, it's like something just isn't connecting

1
2
3
4
5
6
7
You will pass an input file’s name on the command file. Inside the main function, open this input file, read the 
given course’s name followed by its prerequisites, put all prerequisites into a vector of strings temp, and 
instantiate an object of type Course using the name and the vector temp as parameters to the constructor. Test 
each member function: (1) print to the screen the name returned by get_name(), (2) print to the screen all 
prerequisites returned by get_prereq( vector<string> &prereqs), i.e. you have to create a new empty vector of 
strings and pass it to get_prereq. After calling this function, prereqs should contain all prerequisites of the 
course, use for loop to print them to the screen; (3) test print() function. 


Basically I need to write a program that will retrieve strings from a text file. The text file has a single line of text, the first word being a course, the next two being prerequisites to the course. So far I have:

course.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
#include <string>
#include <vector>
using namespace std;


#ifndef COURSE_H
#define COURSE_H

class Course{
   	friend ostream &operator<<(ostream &,  Course &);
	friend ifstream &operator>>(ifstream &, Course &);

	public:
		Course(string aname, vector<string> prereq);
		string get_name(); //member function that returns name
		void get_prereq(vector<string> &prereqs);
        void print();
    private:		
        string name;		
        vector<string> prerequisites;



};

#endif


course_main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;

#include "course.h"

int main( int argc, char *argv[]){
    string t;
    vector<string> temp;

    string input_file = argv[1];
    ifstream inf;
	inf.open(input_file.c_str());
    inf >> name;
    while(inf >> t){
        temp.push_back(t);
    }
    Course course(name, temp);
    inf.close();
}


and course.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<string>
#include<vector>
#include<iostream>
#include<fstream>
#include<sstream>

using namespace std;

#include "course.h"

string Course::get_name(){


}

void Course::get_prereq(vector<string> &prereqs){


}

void Course::print(){
    cout << name << endl;
    for(int i=0; i < prerequisites.size(); i++){
        cout << prerequisites[i];
        }
}



I know that's a big post. If anybody could just point me in the right direction to a post with related info, show me where to get started, or really anything at this point, I'd be incredibly grateful. I love this class (no pun intended) and am really frustrated that I'm not understanding this.
Last edited on
You will pass an input file’s name on the command file.

Use argv[1] to get the filename for the input file. You already did this.

Inside the main function, open this input file, read the given course’s name followed by its prerequisites, put all prerequisites into a vector of strings temp, and instantiate an object of type Course using the name and the vector temp as parameters to the constructor.

Use a vector of strings to read all the data from the input file.
After reading is done, feed the vector to a Course object. You did this too.
Errr... where exactly are you stuck? Hint: next time be clear about it.

Test each member function: (1) print to the screen the name returned by get_name(),


1
2
3
4
5
6
7
8
9
10
// ...

int main(int argc, char *argv[])
{
    // ...

    cout << course.get_name() << '\n';

    // ...
}


(2) print to the screen all prerequisites returned by get_prereq( vector<string> &prereqs), i.e. you have to create a new empty vector of strings and pass it to get_prereq. After calling this function, prereqs should contain all prerequisites of the course, use for loop to print them to the screen;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// ...

int main(int argc, char *argv[])
{
    // need a new vector of strings to feed it to Course::get_prereq()
    vector<string> temp2;

    course.get_prereq(temp2); // copy the Course's vector<string> data into temp2

    for (int i=0; i < temp2.size(); i++)
        cout << temp2[i] << ' ';

    // or if you want the "proper" C++98 way to do this...
    for (vector<string>::const_iterator ci = temp2.begin(); ci != temp2.end(); ++ci)
        cout << *ci << ' ';

    // or the "proper" C++11 way (not supported by Visual Studio 2010)
    for (const string &s: temp2)
        cout << s << ' ';
}


(3) test print() function.


1
2
3
4
5
6
7
8
9
10
// ...

int main(int argc, char *argv[])
{
    // ...

    course.print();

    // ...
}


If you have a decent computer, you may want to upgrade to Visual Studio 2013 Express.
Last edited on
Topic archived. No new replies allowed.