PDA

View Full Version : new to C++


evan
2010-01-25, 02:44
Could someone run me through a Hello world app in C++ on the mac, using the terminal and emacs? not sure if I'm doing something wrong here so I'll just get a complete process and see where I'm deviating.


or if you can answer my specific problem - I've installed Xcode but I can't compile using the g++ thing (as in, running "g++ helloworld.cpp" does nothing - says it doesn't recognize g++, which i've been told to use).

scratt
2010-01-25, 04:52
Apple includes "Hello World!" as the default source file when creating a "C++ tool" project in Xcode.

Or....

Why don't you start with something like this : http://en.wikibooks.org/wiki/Programming_Mac_OS_X_with_Cocoa_for_beginners/Getting_around_in_Xcode_and_Interface_Builder

Cocoa is more relevant to OS X.

But there are also Hello World tutorials out there for C++ on OS X:

http://guides.macrumors.com/Compiling_and_executing_a_Hello_World_application

evan
2010-01-25, 09:56
my class has made it an honor offense to use an IDE on an assignment... (which means that here, I can get kicked out of school for it)

evan
2010-01-25, 11:01
"-bash: g++: command not found"

Banana
2010-01-25, 11:32
it sounds like you don't have the gcc and other files installed, which is the default for a standard Mac OS X. If you have a system DVD handy, on the 2nd disc (I think), there should be an optional installation for Developer Tools which includes XCode, Interface Builder and all those stuff. You don't have to touch XCode & Interface builder but at least you'll get the gcc.

Alternatively, you could go to ADC site and download from there. Last time, I checked, though they did a good job of obscuring the download...

scratt
2010-01-25, 11:51
I think this is a similar thread..

http://stackoverflow.com/questions/1993835/gcc-command-line-on-mac-os-x-with-xcode-2-5

Didn't realise you were restricted to the command line and I tried to make a simple suggestion earlier. Sorry if it wasn't much help.

I am totally confused about the paths on OS X for these tools since I migrated to Snow Leopard and finished some Beta work with a company.. Ever since then my main dev machine has some strange paths, so I can't give advice based on personal experience as my machine is weird.

There seemed to be some changes in the default paths around that time on OS X anyway.. In any case the link above seems to be a good place to start.

evan
2010-01-25, 13:14
thanks guys, I installed xcode when I got my computer a few months ago but I guess it didn't have the gcc for some reason? anyway, I reinstalled and it worked :)

hello world!

bassplayinMacFiend
2010-01-25, 13:25
I think you have to install the "BSD-UNIX Development Tools" in order to get command-line functionality.

ShadowOfGed
2010-01-26, 16:52
my class has made it an honor offense to use an IDE on an assignment... (which means that here, I can get kicked out of school for it)

Someone in charge of your class needs to have their sanity checked.

Banana
2010-01-26, 16:55
Someone in charge of your class needs to have their sanity checked.

Why?

I work with an IDE, but I can see that by putting aside the "clutters" of IDE and focusing on the essentials, it helps one to appreciate the workings of computer better. Else, he's going to get all confused when he encounters an abstraction leak, I'd think.

ShadowOfGed
2010-01-26, 17:23
Why?

I work with an IDE, but I can see that by putting aside the "clutters" of IDE and focusing on the essentials, it helps one to appreciate the workings of computer better. Else, he's going to get all confused when he encounters an abstraction leak, I'd think.

At a basic level, all the IDE does is provide code completion and help make building a project easier.

As far as learning to code, the editor of choice is pretty much immaterial. It's all the same language, and you've got to learn that one way or the other. It just seems like an arbitrary restriction that doesn't really help teach anything. (Unless the goal is to learn g++ flags, but I'm guessing not.)

If the IDE gets in the way of learning the language, it should be left up to the student what tools are appropriate for them. Maybe I prefer editing in vim and compiling with Terminal. In some respects, if you know vim or emacs well enough, they can both act as fairly functional IDEs. So it's a pretty arbitrary distinction, at that.

It just sounds like an I-hate-my-students type of decision that only does them a disservice in the long run, since they'll go into jobs lacking basic skills with IDEs that people expect.

Banana
2010-01-26, 17:29
Well, FWIW, I assumed that was just this class's requirement rather than a general university requirement (which I would quite think very bizarre).

As for IDE's features- while they certainly do make it much easier with the code completion & project building, I again assumed the class was about fundamentals so it was expected that he'd learn about the compiling, linking & creating project by hand mainly to expose what IDE would be doing (and should be- it's a great time-saving feature and there's no reason to be all masochistic about this) so he can better understand how to fix things if they go wrong. If IDE is all he knows, then it's kinda like having goggles on at all times, I would think.

ShadowOfGed
2010-01-26, 18:07
Well, FWIW, I assumed that was just this class's requirement rather than a general university requirement (which I would quite think very bizarre).

As for IDE's features- while they certainly do make it much easier with the code completion & project building, I again assumed the class was about fundamentals so it was expected that he'd learn about the compiling, linking & creating project by hand mainly to expose what IDE would be doing (and should be- it's a great time-saving feature and there's no reason to be all masochistic about this) so he can better understand how to fix things if they go wrong. If IDE is all he knows, then it's kinda like having goggles on at all times, I would think.

True, but none of the programming classes I took ever cared about or covered the details of compilation and linking. Even if you didn't use an IDE, all you knew is that code went in one side, and an executable came out the other. (Shared libraries don't exist. You only ever create executables.)

In a lot of respects, linking and the build process are fairly advanced topics, an would/should be covered by a compilers course in a CS track, not the "Intro to Programming C++" classes. That's why I don't quite understand forbidding an IDE for a basic class like this. But I should get off my soap box. :lol:

Kickaha
2010-01-26, 18:20
True, but none of the programming classes I took ever cared about or covered the details of compilation and linking.

O.o

Dear god, that's like taking an engineering class that doesn't care about gravity. "Oh don't worry, you just make it look pretty, and give the drawings to a construction crew, and they make a building!"

Even if you didn't use an IDE, all you knew is that code went in one side, and an executable came out the other. (Shared libraries don't exist. You only ever create executables.)

So... they kind of failed to teach you how to use the basic tools of the trade? Ouch.

In a lot of respects, linking and the build process are fairly advanced topics, an would/should be covered by a compilers course in a CS track, not the "Intro to Programming C++" classes.

A compilers course teaches you how to *WRITE* a compiler, not *USE* a compiler. Christ, that's like claiming a college level English Lit class is where you learn to *read*.

That's why I don't quite understand forbidding an IDE for a basic class like this. But I should get off my soap box. :lol:

Naw, we're all friends here... ;)

Ryan
2010-01-26, 22:23
True, but none of the programming classes I took ever cared about or covered the details of compilation and linking. Even if you didn't use an IDE, all you knew is that code went in one side, and an executable came out the other. (Shared libraries don't exist. You only ever create executables.)

In a lot of respects, linking and the build process are fairly advanced topics, an would/should be covered by a compilers course in a CS track, not the "Intro to Programming C++" classes. That's why I don't quite understand forbidding an IDE for a basic class like this. But I should get off my soap box. :lol:

The very first intro class in our CS program goes over the basics of linking and compiling. Just enough to give you an overview of the process. Everyone is also required to use vim for that class, taught in C (the second class in the sequence uses Java in Eclipse, the third C++ in Visual Studio). Learning command line tools is a big part of the first class as well, since for most students it's both intro to programming and intro to Linux.

ShadowOfGed
2010-01-26, 23:22
The very first intro class in our CS program goes over the basics of linking and compiling. Just enough to give you an overview of the process. Everyone is also required to use vim for that class, taught in C (the second class in the sequence uses Java in Eclipse, the third C++ in Visual Studio). Learning command line tools is a big part of the first class as well, since for most students it's both intro to programming and intro to Linux.

Interesting. For ours, people were thrown at Visual Studio and had to build everything there. (Wonderful choice of compiler and IDE, but whatever.) It was a bit limiting, but that's mostly because the Windows development tools tend to lag behind.

We had an IDE do most of the work, and people still had a hard time wrapping their heads around C++.

scratt
2010-01-26, 23:33
You only have to google for common Xcode problems online to see why it's a good idea for people to use the command line once or twice in their life for gcc. People just don't get what most of the settings in Xcode do if they've never used gcc in a shell. It's all just bright lights, shiny buttons, and the thing poops a program out as far as they are concerned!

People who jumped straight in with Xcode also don't have a clue where, what or why libraries exist. Which is fine if you're just bunging together a Twitter App in IB. But not if you plan to actually write more than a few thousand lines of code.

So depending on the scope of the course I can see very good reasons for making people do it the "hard way" first. "Wax on Wax off Grasshopper!"

Ryan
2010-01-27, 01:17
Interesting. For ours, people were thrown at Visual Studio and had to build everything there. (Wonderful choice of compiler and IDE, but whatever.) It was a bit limiting, but that's mostly because the Windows development tools tend to lag behind.

We had an IDE do most of the work, and people still had a hard time wrapping their heads around C++.Our department is heavily Linux oriented so they like the students to have a firm grasp of the command line. Often times students don't bother using IDEs as they progress, they stick with the CLI. Last semester I wrote a programming language using vim, lex, yacc and gcc.

Kickaha
2010-01-27, 11:16
Interesting. For ours, people were thrown at Visual Studio and had to build everything there. (Wonderful choice of compiler and IDE, but whatever.) It was a bit limiting, but that's mostly because the Windows development tools tend to lag behind.

We had an IDE do most of the work, and people still had a hard time wrapping their heads around C++.

*nod* C++ is a big massive kitchen sink, and it's a hard learning curve.

To me, and most C++ devs I know, having a solid foundation in what's going on at the low levels (linking, building, pointer arithmetic, registers, etc, etc) is crucial to really having a deep understanding and facility with C++. You just can't escape it.

Now, if it were a Java class, I could see skipping up to the Eclipse IDE, and falling back only when things go wrong (which is way too often in Eclipse), but in C++? Knowing the lower levels is *part* of using C++ effectively.

Ryan
2010-01-27, 14:11
Now, if it were a Java class, I could see skipping up to the Eclipse IDE, and falling back only when things go wrong (which is way too often in Eclipse), but in C++? Knowing the lower levels is *part* of using C++ effectively.

:mad:

I despise Eclipse. Last semester it just up and decided JUnit didn't exist anymore. Never did get it to work right again. I gave up and just did everything on the command line. Good excuse to learn makefiles though.

evan
2010-02-09, 11:04
whoaaaa didn't realize all this talk going on- anyway, another question:

how do I get this error message to go away (and let my code compile):

postfixCalculator.cpp: In function ‘void add()’:
postfixCalculator.cpp:31: error: ‘s’ was not declared in this scope
postfixCalculator.cpp: In function ‘void addNum(int)’:
postfixCalculator.cpp:43: error: ‘s’ was not declared in this scope
postfixCalculator.cpp: In function ‘int getTopValue()’:
postfixCalculator.cpp:47: error: ‘s’ was not declared in this scope


//Evan Davis
//ewd7q
//Feb 9 2010
//postfixCalculator.cpp


#include "postfixCalculator.h"
#include <stack>





PostfixCalculator::PostfixCalculator() {


}
//PostfixCalculator(const PostfixCalculator& original); //Copy Constructor


//PostfixCalculator::~PostfixCalculator() {
// delete s;
//}//Destructor

//PostfixCalculator& operator=(const PostfixCalculator& source); //Equals Operator


//bool isEmpty() const; //Returns true if empty; else false

void add() {
int a = s.top();
s.pop();
int b = s.top();
s.pop();
s.push(a+b);
}
void subtract();
void multiply();
void divide();
void negate();

void addNum(int x) {
s.push(x);
}

int getTopValue() {
int a = s.top();
s.pop();
return a;
}

//Evan Davis
//ewd7q
//Feb 9 2010
//postfixCalculator.h

#ifndef POSTFIXCALC_H
#define POSTFIXCALC_H

#include <iostream>
#include <stack>
using namespace std;

class PostfixCalculator
{
public:
PostfixCalculator(); //Constructor
//PostfixCalculator(const PostfixCalculator& original); //Copy Constructor
//~PostfixCalculator(); //Destructor
//PostfixCalculator& operator=(const PostfixCalculator& source); //Equals Operator

//bool isEmpty() const; //Returns true if empty; else false

void add();
void subtract();
void multiply();
void divide();
void negate();

void addNum(int x);

int getTopValue();
stack<int> s;

private:
//stack<int> s;

};

#endif
/* end of List.h */

//Evan Davis
//ewd7q
//Feb 9 2010
//testPostfixCalc.cpp


#include <iostream>
#include <string>
//#include <stack>
using namespace std;

#include "postfixCalculator.h"

int main() {
PostfixCalculator p;


//12345++++
//PostfixCalculator p;
p.addNum (1);
p.addNum (2);
p.addNum (3);
p.addNum (4);
p.addNum (5);
p.add();
p.add();
p.add();
p.add();
cout << "Result is: " << p.getTopValue() << endl;
//20 10 - -3 10 - - 2 -

//-1 -2 -5 3 * 2 -2 * * * *

//-1512 -12 -2 / / -2 / 3 /

//-1 ~ ~ ~

return 1;
}

obviously it has something to do with how i'm initializing my stack for use in the postfixCalculator... I just can't figure out how or where to do it.

Kickaha
2010-02-09, 11:40
Strip it down... this won't compile either, and with the same error:

#include <stack>

class PostfixCalculator
{
public:
void add();
stack<int> s;
};

void add() {
int a = s.top();
s.pop();
int b = s.top();
s.pop();
s.push(a+b);
}

See why?

evan
2010-02-09, 11:49
Strip it down... this won't compile either, and with the same error:

#include <stack>

class PostfixCalculator
{
public:
void add();
stack<int> s;
};

void add() {
int a = s.top();
s.pop();
int b = s.top();
s.pop();
s.push(a+b);
}

See why?

enlighten me? :D

Kickaha
2010-02-09, 11:57
Ah, nope.

You've declared an add method inside the PostfixCalculator class.

But you've *DEFINED* a *GLOBAL* function named app.

The global function has no freaking clue what s is.

You have to make the global app function scoped within the class.

#include <stack>

class PostfixCalculator
{
public:
void add();
stack<int> s;
};

void
PostfixCalculator::add() {
int a = s.top();
s.pop();
int b = s.top();
s.pop();
s.push(a+b);
}

This clearly states that add is in the scope of the PostfixCalculator class. All class method definitions must be prepended by the proper scoping, otherwise they end up in global scope.

evan
2010-02-09, 12:03
Ah, nope.

You've declared an add method inside the PostfixCalculator class.

But you've *DEFINED* a *GLOBAL* function named app.

The global function has no freaking clue what s is.

You have to make the global app function scoped within the class.

#include <stack>

class PostfixCalculator
{
public:
void add();
stack<int> s;
};

void
PostfixCalculator::add() {
int a = s.top();
s.pop();
int b = s.top();
s.pop();
s.push(a+b);
}

This clearly states that add is in the scope of the PostfixCalculator class. All class method definitions must be prepended by the proper scoping, otherwise they end up in global scope.

thanks a lot. this is what I get for reading AN in class i guess haha.

evan
2010-03-02, 13:13
I'm making a binary expression tree, and to do so I'm reading in values and putting them into a tree node and a pointer to that tree node into a stack. How do I use the STL stack implementation with pointers to my TreeNode class?

relevant code:


stack<TreeNode> s;

TreeNode * a = new TreeNode(val);
s.push(a);


TreeNode is a class I created.

nevermind, problem fizixed

Partial
2010-03-02, 18:30
Where was the stack being instantiated Stack<stronglyTypedOjbect> stack = new Stack<stronglyTypedObject>();

evan
2010-03-16, 09:49
Another question!

I'm working in implementing a hash table, and I'm running into a Seg Fault whenever I try to access my buckets :( Here's the code:

simple test class

//TEST HASH!!

#include "hashTable.h"
#include <iostream>
#include <string>

using namespace std;

int main() {
hashTable htab(101);
htab.insert("blah");
cout << htab.contains("blah") << endl;
cout << htab.contains("bkldsh") << endl;

}

hashTable.h

#ifndef HASHTABLE_H
#define HASHTABLE_H

#include <list>
#include <string>
#include <vector>

using namespace std;

class hashTable {
public:
hashTable(int size);
bool contains(const string & x);
bool insert(const string & x);
private:
vector<list<string> > theLists;
int currentSize;
int tableSize;
int hash(const string & key);
};

#endif


and hashTable.cpp

#include "hashTable.h"
#include <iostream>

#include <list>
#include <string>
#include <vector>

using namespace std;

hashTable::hashTable(int size) {
vector<list<string> > theLists (size);
currentSize = 0;
tableSize = size;
}

bool hashTable::contains(const string & x) {
const list<string> & whichList = theLists[hash(x)];
return find(whichList.begin(), whichList.end(), x) != whichList.end();
}
bool hashTable::insert(const string & x) {
int hashVal = hash(x);
cout << hashVal << endl;
list<string> & iList = theLists[hashVal];
if(find(iList.begin(), iList.end(), x) != iList.end())
return false;
iList.push_back(x);
currentSize++;
return true;
}

int hashTable::hash(const string & key) {
int hashVal = 0;
for(int i = 0; i < key.length(); i++) {
hashVal = 37 * hashVal + key[i];
}

hashVal %= tableSize;

if(hashVal < 0)
hashVal += tableSize;

return hashVal;
}


the Vector that i'm using for my hash table seems to work fine - using couts I determined that I get the segfault whenever I try to do anything with iList inside the insert function (either the if statement or my push_back() call). Anyone know why I'm getting a segfault here?

EDIT: Got it! had to do with the scope of theLists being limited to the constructor... fixed it with


theLists.resize(size);