Like Tree2Likes
  • 2 Post By iMalc

4 LNK errors please help

This is a discussion on 4 LNK errors please help within the C++ Programming forums, part of the General Programming Boards category; I am writing this code for class, it is still in progress. I am trying to compile so I can ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    4 LNK errors please help

    I am writing this code for class, it is still in progress. I am trying to compile so I can test out the code I have so far. There is code that I temporarily turned into comments to try to fix my compile errors, but no luck. Here are the errors I come up with using Visual Studio.


    main.obj : error LNK2028: unresolved token (0A00045A) "public: __thiscall Stack::Stack(void)" (??0Stack@@$$FQAE@XZ) referenced in function "public: void __thiscall Stack::listCommon(class Stack,class Stack)" (?listCommon@Stack@@$$FQAEXV1@0@Z)

    1>main.obj : error LNK2028: unresolved token (0A00045B) "public: __thiscall Stack::~Stack(void)" (??1Stack@@$$FQAE@XZ) referenced in function "public: void __thiscall Stack::listDup(class Stack)" (?listDup@Stack@@$$FQAEXV1@@Z)

    1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack::~Stack(void)" (??1Stack@@$$FQAE@XZ) referenced in function "public: void __thiscall Stack::listDup(class Stack)" (?listDup@Stack@@$$FQAEXV1@@Z)

    1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack::Stack(void)" (??0Stack@@$$FQAE@XZ) referenced in function "public: void __thiscall Stack::listCommon(class Stack,class Stack)" (?listCommon@Stack@@$$FQAEXV1@0@Z)

    1>c:\users\mschmidt\documents\visual studio 2010\Projects\main\Debug\main.exe : fatal error LNK1120: 4 unresolved externals

    Code:
    #include"stack.h"
    #include"collection.h"
    #include<iostream>
    usingnamespace std;
    int main() {
      Stack a, b;
      int choice, select, add;
      for( ; ;) {
        do{
          cout << "Please choose from the following: " << endl;
          cout << "1. Add to stack" << endl;
          cout << "2. Find duplicates in stack" << endl;
          cout << "3. Find common elements between stacks A and B" << endl;
          cin >> choice;
        } while(choice < 1 || choice > 3 && choice != 4);
        if(choice == 4) break; 
          switch(choice) {
          case 1:
            cout << "Which stack would you like to add to?" << endl;
            cout << "1. Stack A" << endl;
            cout << "2. Stack B" << endl;
            cin >> select;
            if(select == 1) {
              cout << "Please enter integer to add to stack A." << endl;
              cin >> add;
              //a.insert(add);
            }
            elseif(select == 2) {
              cout << "Please enter integer to add to stack B." << endl;
              cin >> add;
              //b.insert(add);
            }
            else cout << "Invalid choice";
            break;
          case 2:
            select = 0;
            cout << "Which stack would you like to see the duplicates of?" << endl;
            cout << "1. Stack A" << endl;
            cout << "2. Stack B" << endl;
            cin >> select;
            if(select == 1) {
              a.listDup(a);
            }
            elseif(select == 2) {
              b.listDup(b);
            }
            else cout << "Invalid selection!" << endl;
            break;
          }
       }
    }
    
    Code:
    #ifndef STACK_H
    #define STACK_H
    #include<iostream>
    #include"collection.h"
    class Stack : public Collection {
    public:
    Stack();
    //Stack insert(int i);
    //void remove(Stack ob1);
    void listDup(Stack ob1);
    void listCommon(Stack ob1, Stack ob2);
    ~Stack();
    };
    //Stack Stack::insert(int i) {
    // Stack newset;
    // if(length==MaxSize) {
    // cout << "Set is full." << endl;
    // return *this;
    // }
    // newset = *this;
    // newset.members[newset.length] = i;
    // newset.length++;
    // return newset;
    //}
    //void Stack::remove(Stack ob1) {
    // Stack newset;
    // ob1.length--;
    // for(int i = 0; i < ob1.length; i++) {
    // newset.members[newset.length] = ob1.members[i];
    // }
    //}
    void Stack::listDup(Stack ob1) {
      std::cout << "Here are the duplicates in the stack: { ";
      for(int i = 0; i < ob1.length; i++)
        for(int j = i+1; j < ob1.length; j++)
          if(ob1.members[i] == ob1.members[j])
            std::cout << ob1.members[i] << " }" << std::endl;
    }
    void Stack::listCommon(Stack ob1, Stack ob2) {
      Stack newset;
      int k = 0;
      for(int i=0; i<ob1.length; i++) {
        for(int j=0; j<ob2.length; j++) {
          if(ob1.members[i] == ob2.members[j]) {
            newset.members[k] = ob1.members[i];
            k++;
          }
        }
      }
    d::cout << "The common elements of the stacks are: ";
    newset.colDisplay();
    }
    #endif
    
    Code:
    #ifndef COLLECTION_H
    #define COLLECTION_H
    #include<iostream>
    constint MaxSize = 100;
    class Collection {
    
    protected:
    int length;
    int members[MaxSize];
    int find(int value);
    public:
    Collection() {length = 0;}
    int getSize() {return length;}
    void incSize() {length++;}
    bool isMember(int i);
    void colDisplay();
    ~Collection() { };
    };
    int Collection::find(int value) {
      for(int i=0; i<length; i++)
        if(members[i] == value) return i;
          return -1;
    }
    bool Collection::isMember(int i) {
      if(find(i) != -1) returntrue;
        returnfalse;
    }
    void Collection::colDisplay() {
      std::cout << "{ ";
      for(int i=0; i<length; i++)
        std::cout << members[i] << " ";
        std::cout << "}" << std::endl;
    }
    #endif
     
    
    Attached Files Attached Files
    Last edited by Mike Schmidt; 10-23-2012 at 02:07 PM.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    You told the compiler you were going to provide an implementation of a default constructor and a destructor for the Stack class. It believed you, but the linker caught you in your lie.

    And it's a bad idea to put code in a header file.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Ah I did not catch that. Thank you. Can you please elaborate on not using code in headers. How would I define my class functions if it is not a good idea to use code?

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    You define the class in the header. You put each class's implementation in a separate C++ file, which you add to your project. All member function implementations go in their class's implementation file.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Ok I will try to do that in the future. Thank you for your help!

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Note that it's bad practice to provide a constructor or destructor if your class does not require them. But if it does require a destructor then it probably requires other stuff too.
    Look up and read about "The Rule of Three C++".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. errors
    By sniper22 in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2006, 08:14 AM
  2. Help with ISO errors
    By alpha22 in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 05:10 PM
  3. Tic-Tac-Toe errors
    By fuh in forum C++ Programming
    Replies: 15
    Last Post: 06-21-2003, 04:05 PM
  4. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 09:43 PM
  5. Qt3 Errors
    By JasonLikesJava in forum Linux Programming
    Replies: 17
    Last Post: 05-10-2002, 11:17 AM

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