I'm a newbie programmer, and I'm also a newbie windows programmer. I have used Mac for most of my career so I'm used to using the awesome command line tools and g++, in spite of bad things people have to say about it.
I'm trying to understand how to use pointers, so I wrote (mostly stole code) based on one of the articles from the Stanford computer science series.
This code, I don't think I changed it in any way, compiles and works fine on (ignore the lack of garbage collection I'm not there yet) Mac. I don't remember doing anything to the object files. In Visual C++ on windows 7 it gives me
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>C:\Users\me\Desktop\linkedliststuff\vctest\Debug\vctest.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
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
|
#ifndef LL_H
#define LL_H
class linkedlist
{
public:
//initializer/destructor
linkedlist();
~linkedlist();
//functional
struct node
{
int n;
node *next;
} *top,*holder;
void add(struct node*& a, int b);
void incorder(struct node*& a);
void decorder(struct node*& b);
int length(struct node* a);
void populate(struct node*& a,int b);
void push(int a);
void pop();
void print();
void increment(int i);
};
#endif
|
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
|
#include "linkedlist.h"
#include <stdlib.h>
#include <stdio.h>
linkedlist::linkedlist()
{
top = new node;
}
linkedlist::~linkedlist()
{
}
void linkedlist::add(struct node*& a, int b)
{
node* newNode;
newNode = new node;
newNode->n = b;
newNode->next = a;
a = newNode;
}
int linkedlist::length(node* a)
{
node* b = a;
int i = 0;
while (a!=0)
{
a=a->next;
i++;
}
return i;
}
void linkedlist::incorder(struct node*& a)
{
}
void linkedlist::decorder(struct node*& b)
{
}
void linkedlist::populate(struct node*& a,int b)
{
while(b>0)
{
node* g;
g = new node;
g->n = b;
g->next = a;
a = g;
b--;
}
}
void linkedlist::increment(int i)
{
populate(top,i);
}
void linkedlist::push(int a)
{
add(top,a);
}
void linkedlist::pop()
{
printf("num: %d\n",top->n);
top=top->next;
}
void linkedlist::print()
{
node* a;
a = new node;
for(a=top;a!=0;a=a->next)
{
printf("num: %d\n",a->n);
}
}
|
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
|
#include <cstdlib>
#include <iostream>
#include <string>
#include "linkedlist.h"
using namespace std;
int main()
{
int i;
string b;
linkedlist c;
while(b!="quit")
{
cout << "Enter command: ";
cin >> b;
if(b!="quit")
{
if(b=="push")
{
cout << "Enter number: ";
cin >> i;
c.push(i);
}
else if(b=="pop")
{
c.pop();
}
else if(b=="populate")
{
cout << "Enter number: ";
cin >> i;
c.increment(i);
}
else if(b=="print")
{
c.print();
}
}
}
return 0;
}
|