如何让构造函数与函数正常工作,使main()显示字符串和int数据并解决未解析外部符号错误?
Hey there! Let's figure out that unresolved external symbol error you're dealing with—this is a super common gotcha when working with C++ classes, and it has nothing to do with your void function not needing a return value (you're totally right about that part, by the way!).
What's causing the error?
That "unresolved externals" message means your linker can't find the actual implementation of your showInfo() function (or maybe your constructor!). Usually this happens when:
- You declared the function inside your class, but never wrote the actual code for it outside the class without the proper scope resolution.
- You split your code into header and source files but forgot to compile/link the source file with your main code.
Let's fix this with a complete example
Here's a working version that does exactly what you want: initializes a class with a string and int via constructor, then uses a void function to display those values.
Single-file implementation (easy for testing)
#include <iostream> #include <string> // Our class declaration class MyData { private: // Private member variables (initialized by constructor) std::string m_message; int m_number; public: // Constructor declaration MyData(std::string msg, int num); // Void function declaration to display info void displayInfo(); }; // Constructor implementation (note the MyData:: scope!) MyData::MyData(std::string msg, int num) : m_message(msg), m_number(num) // Initialization list (more efficient than assignment) {} // HERE'S THE PART YOU PROBABLY MISSED! // Implementation of the void display function, with proper scope void MyData::displayInfo() { std::cout << "Stored String: " << m_message << "\n"; std::cout << "Stored Integer: " << m_number << "\n"; } int main() { // Create an instance of our class with sample data MyData myObj("Hello from my custom class!", 42); // Call the void function to display the data myObj.displayInfo(); return 0; }
If you're using separate header/source files
If you split your code into MyData.h and MyData.cpp, make sure:
- MyData.h has the class declaration:
#pragma once #include <string> class MyData { private: std::string m_message; int m_number; public: MyData(std::string msg, int num); void displayInfo(); };
- MyData.cpp has the implementations:
#include "MyData.h" #include <iostream> MyData::MyData(std::string msg, int num) : m_message(msg), m_number(num) {} void MyData::displayInfo() { std::cout << "String: " << m_message << "\nInt: " << m_number << "\n"; }
- When compiling, include both files:
g++ main.cpp MyData.cpp -o my_program
Key takeaways
- Your understanding of
voidfunctions is correct—they don't return a value, so that's not the issue here. - The linker error happens because you didn't provide the actual code for your member function(s) outside the class (or didn't link the file that has it).
- Always use
ClassName::when implementing class members outside the class declaration to tell the compiler which class the function belongs to.
内容的提问来源于stack exchange,提问作者SysAdminGrasshopper

